R语言 数据操作小贴士合集
#创建空的数据框
data <- data.frame(ID= character(), age= numeric(), stringsAsFactors=FALSE)
#排序
data2 <- data[order(data[,18],decreasing=F),] #将data数据集按照第18列的值升序排列命名为数据集data2
#删除空行
data <- data[complete.cases(data[,5:6]),]#删除第五六列有空值的行
data <- na.omit(data)#删除有空值的行
#左联1
data_merge <- (merge(data_name, data_inform, by = 'id'))
#左联2
library(plyr)
data_join <- join(data_name, data_inform, by = "id")
#左联3
library(sqldf)
data_sqldf1 <- sqldf(
"SELECT data_name.name, data_inform.age
FROM data_name INNER JOIN data_inform
ON ladata_name.id= data_inform.id"
)
data_sqldf2 <- sqldf("SELECT * FROM data_name LEFT JOIN data_inform USING (id)")
#指定列缺失值填充
a[is.na(a[,16]),16] <- 0 #16列为空的行,将16列填充为0
#剔重
test[!duplicated(test),]
#重命名指定列
colnames(data)[2] <- 'newname'