加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

LearningR-数据处理赏罚

发布时间:2021-03-14 19:47:13 所属栏目:大数据 来源:网络整理
导读:R自带函数 reshape2 data restructuring dplyr data aggregation tidyr 待清算 字符串处理赏罚 1. R自带函数 1.1 转置 行使函数t()可对一个矩阵或数据框举办转置,对付数据框,行名将酿成变量(列)名。 cars - mtcars(1:5,1:4)carst(cars) 数列array举办维度转

注: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和acast

Use acast or dcast depending on whether you want vector/matrix/array output or data frame output. Data frames can have at most two dimensions.

  1. dcast——返回的功效是一个数据框

  2. acast——返回的功效可所以向量、矩阵可能数组

挪用名目为:

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. dplyr

3.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 分列 arrange

arrange(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的非凡函数

  • starts_with(x,ignore.case = TRUE): names starts with x

  • ends_with(x,ignore.case = TRUE): names ends in x

  • contains(x,ignore.case = TRUE): selects all variables whose name contains

  • matches(x,ignore.case = TRUE): selects all variables whose name matches the regular expression x

  • num_range("x",width = 2): selects all variables (numerically) from x01 to x05.

  • one_of("x","y","z"): selects variables provided in a character vector.

  • everything(): selects all variables.

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

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读