使用R语言-操作data.frame

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})

10 数据查询

  10.1 根据条件查询

  查询taste为good的所有行

trainSet2[trainSet2$taste == "good",]

  查询taste为good的指定行

trainSet2[trainSet2$taste == "good",1]
trainSet2[trainSet2$taste == "good",1:2]
trainSet2[trainSet2$taste == "good",c("size","color")]

  也可以用which,跟上面的类似

trainSet2[which(trainSet2$taste == "good"),c("size","color")]

  使用subset,会简化查询,可以指定条件,指定选择列

subset(trainSet2,taste == "good" & newc1 < 5,select = c("size","taste"))

11 使用sql查询data frame

  对于熟悉sql的是个福音啊

  11.1 安装sqldf

install.packages('sqldf')

  11.2 引入sqldf

library(sqldf)

  11.3 查询示例

result <- sqldf("select * from trainSet2 where size='大'")

  11.4 链接合并示例,两个表根据newc1 和 fkey进行链接

trainSet3 <- data.frame(fkey = c(1,2,3,4),newc3=c("a","b","c","d"))
resdult <- merge(trainSet2,trainSet3,by.x = "newc1",by.y = "fkey")
  newc1 size weight color taste newc2 newc3
1     1   大     轻    红  good    红     a
2     2   小     重    红  good    红     b
3     3   大     轻    红   bad    红     c
4     4   大     轻    绿   bad    绿     d