LearningR-数据处理
注:reshape包的重铸函数为cast(),reshape2包的重铸函数为dcast()和acast() #数据集mydata ID <- c(1,2) Time <- c(1,2) X1 <- c(5,6,2) X2 <- c(6,4) mydata <- data.frame(ID,Time,X1,X2) 2.1融合-melt数据集的融合是将它重构为这样一种格式:每个测量变量独占一行,行中带有要唯一确定这个测量所需的标识符变量。 library(reshape2) md <- melt(mydata,id=c("ID","Time")) md <- melt(mydata,id=1:2) 2.2重铸-dcast和acastUse acast or dcast depending on whether you want vector/matrix/array output or data frame output. Data frames can have at most two dimensions.
调用格式为: newdata <- dcast(data,formula,fun.aggregate = NULL,...,margins = NULL,subset = NULL,fill = NULL,drop = TRUE,value.var = guess_value(data)) newdata <- acast(data,value.var = guess_value(data)) 其中md为已融合的数据,formula描述想要的结果,FUN是(可选的)数据整合函数。 rowvar1 + rowvar2 + ... ~ colvar1 + colvar2 + ... 在这个公式中,rowvar1 + rowvar2 + ... 定义了要划掉的变量集合,以确定各行的内容,而colvar1 + colvar2 + ... 则定义了要划掉的、确定各列内容的变量集合。 #执行整合 acast(md,ID~variable,mean) dcast(md,tTime~variable,ID~Time,mean) #不执行整合 dcast(md,ID+Time~variable) dcast(md,ID+variable~Time) dcast(md,ID~variable+Time) 2.3 练习library(reshape2) head(airquality) mydata <- airquality mydata1 <- melt(mydata,id=c("Month","Day"),variable.name = "type",value.name = "val") #选定测量变量为Ozone、Wind mydata2 <- melt(mydata,measure = c("Ozone","Wind"),value.name = "val") str(mydata1) str(mydata2) #大写转换为小写 names(mydata) <- tolower(names(mydata)) a <- melt(mydata,id=c("month","day"),na.rm=TRUE) #数据b和原始数据airquality一样,数据复原了。 b <- dcast(a,month + day ~variable) result1 <- dcast(a,month ~variable,mean) #查看缺失值数量的函数 myfun <- function(x){return(sum(is.na(x)))} result2 <- dcast(a,myfun) result3 <- melt(mydata,"day")) result4 <- dcast(result3,myfun) result5 <- recast(mydata,month ~ variable,id.var = c('month','day'),fun = myfun) 3. dplyr3.1 基本操作3.1.1 数据类型将过长过大的数据集转换为显示更友好的 tbl_df 类型 library(dplyr) iris_df <- tbl_df(iris) 3.1.2 筛选filter按给定的逻辑判断筛选出符合要求的子数据集,类似于 base::subset() 函数 filter(iris_df,Species == 'setosa',Sepal.Length >=5) filter(iris_df,Species == 'setosa' & Sepal.Length >=5) 用R自带函数实现: iris_df[iris_df$Species == 'setosa' & iris_df$Sepal.Length >=5,] 除了代码简洁外,还支持对同一对象的任意个条件组合,如: filter(iris_df,Species == 'setosa' | Sepal.Length >=5) 注意: 表示 AND 时要使用 & 而避免 && 3.1.3 排列 arrangearrange(iris_df,Sepal.Length,Sepal.Width) arrange(iris_df,desc(Sepal.Length)) #这个函数和 plyr::arrange() 是一样的,类似于 order() 用R自带函数实现: iris_df[order(iris_df$Sepal.Length,iris_df$Sepal.Width),] iris_df[order(desc(iris_df$Sepal.Length)),] 3.1.4 选择select用列名作参数来选择子数据集: select(iris_df,1:2) select(iris_df,Species,Sepal.Width) select(iris,everything()) #重命名列名 select(iris_df,Length=Sepal.Length,Width=Sepal.Width) select(iris_df,petal = starts_with("Petal")) 排除列名: select(iris_df,-Petal.Length,-Petal.Width) select的特殊函数
select(iris_df,everything()) select(iris_df,starts_with("Petal")) select(iris_df,ends_with("Width")) select(iris_df,contains("etal")) select(iris_df,matches(".t.")) #选取名称符合指定表达式规则的列 select(iris_df,Sepal.Length:Petal.Width) select(iris_df,Petal.Length,Petal.Width) vars <- c("Petal.Length","Petal.Width") select(iris_df,one_of(vars)) df <- as.data.frame(matrix(runif(100),nrow = 10)) df <- tbl_df(df) select(df,V4:V6) select(df,num_range("V",4:6)) ":" 选择连续列,contains来匹配列名 同样类似于R自带的subset() 函数. subset(iris,select=c(1,2)) subset(iris,select=c(3,4)) subset(iris,select=c(Petal.Length,Petal.Width)) (编辑:源码网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |