标签:
1 向一个data.frame指定列插入一列新数据
1.1 插入一列到指定位置
y<-1:4 data1 <-data.frame(x1=c(1,3,5,7), x2=c(2,4,6,8),x3=c(11,12,13,14),x4=c(15,16,17,18)) data2<-cbind(data1[,1:2],y,data1[,3:ncol(data1)])
插到data1末尾
data2<-cbind(data1,y)
插到第一列
data2<-cbind(y,data1)
2 向一个data.frame指定行插入一行新数据
2.1 插入一行到指定位置
data1<- data.frame(x1=runif(10),x2= runif(10),x3= runif(10)) row<- c(1, 1, 1) data2<- rbind(data1[1:5,], row, data1[6:nrow(data1), ])
插入到data1末尾
data2<- rbind(data1, row)
插入到data1第一行
data2<- rbind(row, data1)
3 给data frame设置列名
colnames(data2) <- c(‘row1‘,‘row3‘,‘row3‘)
4 创建一个空的data frame,并制定列名(方法有点挫,目前还没找到其他方式)
4.1 创建一个包含一行的,然后用-1取
emptyDF <- data.frame(row1 = c(NA), row2 = c(NA))
emptyDF <- emptyDF[-1,]
4.2 通过matrix创建data frame
emptyDF <- data.frame(matrix(c(NA), byrow = TRUE,dimnames = list(day = c(),condition = c("outlook","temperature","humidity","wind")), nrow=0, ncol=4))
5 创建一个data frame,并查看
trainSet2 <-data.frame( size=c("大","小","大","大","小","小"), weight=c("轻","重","轻","轻","重","轻"), color=c("红","红","红","绿","红","绿"), taste=c("good","good","bad","bad","bad","good") )
size weight color taste 1 大 轻 红 good 2 小 重 红 good 3 大 轻 红 bad 4 大 轻 绿 bad 5 小 重 红 bad 6 小 轻 绿 good
6 查看行名
row.names(trainSet2)
7 查看列名
colnames(trainSet2)
8 数据访问
访问第一行
trainSet2[1,]
访问第一列
trainSet2[,1]
trainSet2$size
trainSet2[[1]]
trainSet2[["size"]]
访问多行,例如1,2行
trainSet2[1:2,]
访问多列,例如1,2列
trainSet2[,1:2]
trainSet2[c("size","weight")]
9 添加新列
trainSet2$newc1 <- c(1,2,3,4,5,6)
trainSet2 <- within(trainSet2,{newc2 <- color})
标签:
原文地址:http://www.cnblogs.com/liuchangchun/p/4457478.html