总结一下“入门3R”(Reading, ‘Riting, ‘Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的。
vector
命名
1 2
|
month.days<-c(31,28,31,30,31,30,31,31,30,31,30,31) names(month.days)<-month.name
|
操作文本
1.文本分离
1 2
|
pangram<-"The quick brown fox jumps over the lazy dog" strsplit(pangram," ")
|
strplit()
函数将pangram
用空格切开,这个函数的返回值是list
1
|
words<-strsplit(pangram," ")[[1]]
|
可以取出字符串数组
2.文本连接
1 2
|
paste(LETTERS[1:5],1:5,sep="_",collapse="---") paste("Sample",1:5)
|
用空格连接words
中的元素,paste()
接收的参数应该是多个变量,sep
决定多个向量之间的连接符,而collapse
决定统一向量中的元素怎么合并。
3.文本排序
1
|
sort(letters,decreasing=TRUE)
|
4.查找文本
1 2
|
substr(state.name,start=3,stop=6) grep("New",state.name)
|
grep(pattern,x)
返回的是符合pattern的元素的在x中的位置
5.文本替换
1
|
gsub("cheap","sheep's","A wolf in cheap clothing")
|
1 2
|
x<-c("file_a.csv","file_b.csv","file_c.csv") y<-gsub("file_","",x)
|
因子分类
factor(x,levels,labels)
可以创建R因子,而levels
指的是x的输入值,labels
表示新创建的因子的输出值。
因子转换
1 2 3 4 5
|
numbers<-factor(c(9,8,10,8,9)) str(numbers) as.character(numbers) as.numeric(numbers) as.numeric(as.character(numbers))
|
有序因子
类别数据的统计
有序变量
- 使用
factor()
函数,并且指定参数ordered=TRUE
- 使用
ordered()
函数
matrix
1 2 3 4 5
|
matrix(data,ncol,nrow,byrow) dim() rbind() cbind() cbind(1:3,4:6,matrix(7:12,ncol=2))
|
索引、修改和命名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
first.matrix<-matrix[1:12,ncol=4,byrow=TRUE]
first.matrix[1:2,2:3] first.matrix[2:3,] first.matrix[-2,-3] first.matrix[-c(1,3),] first.matrix[2, ,drop=大专栏 array, matrix, list and dataframeeral">FALSE]
first.matrix[3,2]<-4 first.matrix[2,]<-c(1,3) first.matrix[1:2,3:4]<-c(8,4,2,1)
rownames(x)<-c('a', 'b') colnames(x)<-c('c', 'd') colnames(x)[1]<-'aa' x['b',]
|
计算
array
向量和矩阵都是数组.
1 2
|
array(1:24,dim=c(3,4,2)) dim(x)<-c(3,4,2)
|
data.frame
由矩阵创建 x.df<-as.data.frame(x)
由向量创建 data<-data.frame(x,y,z)
如果创建的变量是字符串类型,R会自动转换成因子,可以用stringAsFactor=FALSE
保持字符串类型
1 2
|
names(data)[2]<-'B' rownames(data)<- c('a','b','c')
|
操作data.frame中的值
data.frame并不是向量,而是一组向量列表。但是数据操作时可以当做矩阵来处理,访问单个变量时可以用$
,访问多个变量时可以用[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
y<-rbind(x,new.obs) y<-rbind(x,'d'=new.obs)
new.obs<-data.frame(A=c(1,2),B=c(2,3)) rownames<(new.obs)<-c('e','f') y<-rbind(x,new.obs)
x[c('e','f'),]<-matrix(c(1,1,2,4),ncol=2)
x$C<-new.var new.df<-data.frame(newvar1,newvar2) x<-cbind(x,new.df)
|
list
1 2 3 4 5 6 7 8
|
new.list<-list(x,y) new.nlist<-list(name1=x,name2=y) names(new.nlist) length(new.list)
|
提取列表中的元素
- 使用
[[]]
返回元素本身
- 使用
[]
返回选定元素的列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
new.nlist[[1]]<-x new.nlist[['name1']]<-x new.nlist$name1<-x new.nlist[1]<-list(x) new.nlist[1:2]<-list(x,y)
new.nlist[[1]]<-NULL new.nlist[['name1']]<-NULL new.nlist$name1<-NULL new.nlist[1]<-list(NULL)
new.nlist$name3<-z new.nlist[['name3']]<-z new.nlist['name3']<-list(z)
z<-list(z) c(new.nlist,z)
|