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