1 Basic Building Blocks
?c
> z <- c(1.1, 9, 3.14)
> c(z, 555, z)
[1] 1.10 9.00 3.14 555.00 1.10 9.00 3.14
> z * 2 + 100
[1] 102.20 118.00 106.28
> c(1, 2, 3, 4) + c(0, 10)
[1] 1 12 3 14
> c(1, 2, 3, 4) + c(0, 10, 100)
[1] 1 12 103 4
Warning message:
In c(1, 2, 3, 4) + c(0, 10, 100) :
longer object length is not a multiple of shorter object length
2 Workspace and Files
- getwd() 获取当前工作空间路径
- setwd("dirname") 重设工作空间路径
- ls() 查看工作目录下所有对象
- list.files()或dir() 列出工作目录所有文件
- args(functionname) 查看函数参数
- dir.create("dirname") 新建目录(递归创建:recursive参数要设置为TURE)
- file.create("filename") 新建文件(文件名带后缀)
- file.exists("filename") 检验文件是否存在
- file.info("filename") 获取文件信息
- file.info("filename")$item 获取文件具体某项信息
- file.rename("form_filename", "to_filename") 文件重命名
- file.remove("filename") 删除文件
- file.copy("form_filename", "to_filename") 复制文件
- file.path("filename") 查看相对路径
3 Sequences of Numbers
> 1:20
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> pi:10
[1] 3.141593 4.141593 5.141593 6.141593 7.141593 8.141593 9.141593
> 15:1
[1] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
## ,使用 ?`:`查看帮助
> seq(1, 20)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> seq(0, 10, by = 0.5)
[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5
[17] 8.0 8.5 9.0 9.5 10.0
> my_seq <- seq(5, 10, length = 30)
> 1:length(my_seq)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[29] 29 30
> seq(along.with = my_seq)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[29] 29 30
> seq_along(my_seq)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[29] 29 30
> rep(0, times = 40)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> rep(c(0, 1, 2), times = 10)
[1] 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
> rep(c(0, 1, 2), each = 10)
[1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
4 Vectors
> num_vect >= 6
[1] FALSE TRUE FALSE TRUE
> my_char <- c("My", "name", "is")
> my_name <- c(my_char, "Sylvia")
> my_name
[1] "My" "name" "is" "Sylvia"
> paste(my_name, collapse = " ")
[1] "My name is Sylvia"
> paste("Hello", "world!", sep = " ")
[1] "Hello world!"
> paste(1:3, c("X", "Y", "Z"), sep = "")
[1] "1X" "2Y" "3Z"
> paste(LETTERS, 1:4, sep = "-")
[1] "A-1" "B-2" "C-3" "D-4" "E-1" "F-2" "G-3" "H-4" "I-1" "J-2" "K-3" "L-4" "M-1" "N-2"
[15] "O-3" "P-4" "Q-1" "R-2" "S-3" "T-4" "U-1" "V-2" "W-3" "X-4" "Y-1" "Z-2"
> paste(1:10,sep=",")
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
> paste(1:10,1,sep=",")
[1] "1,1" "2,1" "3,1" "4,1" "5,1" "6,1" "7,1" "8,1" "9,1" "10,1"
> paste(1:10,collapse=",")
[1] "1,2,3,4,5,6,7,8,9,10"
> paste(1:10,1,collapse=",")
[1] "1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10 1"
> paste(1:10,step=",")
[1] "1 ," "2 ," "3 ," "4 ," "5 ," "6 ," "7 ," "8 ," "9 ," "10 ,"
> paste(1:10,1,step=",")
[1] "1 1 ," "2 1 ," "3 1 ," "4 1 ," "5 1 ," "6 1 ," "7 1 ," "8 1 ," "9 1 ," "10 1 ,"
> paste(1:10,1)
[1] "1 1" "2 1" "3 1" "4 1" "5 1" "6 1" "7 1" "8 1" "9 1" "10 1"
5 Missing Values
> x <- c(44, NA, 5, NA)
> x * 3
[1] 132 NA 15 NA
- is.na() 判定是否NA,返回逻辑值(不能使用my_data == NA,因为NA不是一个值)
- sum() 计算值为TRUE的数量
> my_na <- is.na(my_data)
> sum(my_na)
[1] 48
> 0 / 0
[1] NaN
> Inf / Inf
[1] NaN
> Inf - Inf
[1] NaN
6 Subsetting Vectors
> x[1:10]
[1] 1.0368761 NA NA 1.3890263 -2.6372952 NA NA
[8] -0.7592451 NA NA
> x[c(3, 5, 7)]
> x[c(-2, -10)]
> x[-c(2, 10)]
- 对vector取大于零的子集需要先排除缺失值,NA > 0返回NA
> x[x > 0]
[1] 1.03687611 NA NA 1.38902626 NA NA NA
[8] NA NA NA NA 0.03432245 0.35275056 0.72752453
[15] NA NA NA 0.29281794 0.15857674 NA NA
[22] 1.70880097 NA NA NA 0.44062886 0.13357081 NA
[29] NA NA
> x[!is.na(x) & x > 0]
[1] 1.03687611 1.38902626 0.03432245 0.35275056 0.72752453 0.29281794 0.15857674
[8] 1.70880097 0.44062886 0.13357081
- identical(x, y)判断两个对象是否一致
- names()给对象命名,查看命名
> vect <- c(foo = 11, bar = 2, norf = NA)
> names(vect)
[1] "foo" "bar" "norf"
> vect2 <- c(11, 2, NA)
> names(vect2) <- c("foo", "bar", "norf")
> identical(vect, vect2)
[1] TRUE
> vect["bar"]
bar
2
> vect[c("foo", "bar")]
foo bar
11 2
7 Matrices and Data Frames
- dim()返回或设定对象尺寸(vector默认为NULL)
- length()返回对象长度
- class()查看对象种类
> dim(my_vector) <- c(4, 5)
> attributes(my_vector) ##另一个查看dim属性的方法
$dim
[1] 4 5
> class(my_vector)
[1] "matrix"
- matrix()创建矩阵
- cbind()根据列进行合并,合并的前提是所有数据行数相等
- data.frame()创建数据框
- colnames()给数据框定义表头
> my_matrix2 <- matrix(1:20, 4, 5)
my_data <- data.frame(patients, my_matrix)
> cnames <- c("patient", "age", "weight", "bp", "rating", "test")
> colnames(my_data) <- cnames
8 Logic
- &运算符会和vector所有元素比较,&&则只和第一个元素比较