使用在dplyr中返回多个输出的函数将多个列添加到data.frame [英] Add multiple columns to data.frame using a function that returns multiple outputs in dplyr
本文介绍了使用在dplyr中返回多个输出的函数将多个列添加到data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
df<-data.frame(group = rep(c('group1','group2'),50),x = rnorm(100),y = rnorm(100))df%>%collect('variable','value',-group)%&%;%group_by(组,变量)%&%;%summarise(中位数=舍入(分位数(value,0.5,na.rm = T),2),iqr25 =圆(quantile(value,0.25,na.rm = T),2),iqr75 =舍入(分位数(value,0.75,na.rm = T),2))
输出
#小贴士:4 x 5#个群组:群组[2]组变量中位数iqr25 iqr75< fct>< chr>< dbl>< dbl>< dbl>1组1 x 0.06 -0.74 1.042组1 y -0.36 -1.03 0.453组2 x -0.04 -0.85 0.624组2是0.06 -0.56 0.89
可以在不编写3次分位数函数的情况下完成此汇总步骤吗?
我在使用它方面做了一些工作.但是有什么好方法吗?
df%>%collect('variable','value',-group)%&%;%group_by(组,变量)%&%;%summarise(s = toString(round(quantile(value,c(0.25,0.5,0.75),na.rm = T),2)))%>%分隔成= c('q25','median','q75'),sep =',')
解决方案
您可以在 group_by
之后嵌套
数据,然后 map
到分位数
df%>%collect('variable','value',-group)%&%;%group_by(组,变量)%&%;%nest()%&%;%mutate(quant = map(data,〜quantile(.$ value,probs = c(0.25,0.5,0.75))),量化= map(quant,t),量化=地图(quant,as.data.frame),quant = map(quant,setNames,c("iqr25","median","iqr75")),)%&%;%嵌套(数量)%&%;%选择(-数据)#小动作:4 x 5组变量iqr25中值iqr75< fct>< chr>< dbl>< dbl>< dbl>1组1 x -0.876 -0.173 0.4712组2 x -0.372 0.0507 0.5193组1 y -0.785 -0.109 0.6184组2 y -0.944 -0.117 0.647
I am calculating median and values for different quantiles for a continuous variable. I want to add all the columns in a single step. Is this possible to do this. Following is a reproducible example.
df <- data.frame(group = rep(c('group1','group2'),50),
x = rnorm(100),
y = rnorm(100))
df %>%
gather('variable','value', -group) %>%
group_by(group, variable) %>%
summarise(median = round(quantile(value,0.5, na.rm = T),2),
iqr25 = round(quantile(value,0.25, na.rm = T),2),
iqr75 = round(quantile(value,0.75, na.rm = T),2))
OUTPUT
# A tibble: 4 x 5
# Groups: group [2]
group variable median iqr25 iqr75
<fct> <chr> <dbl> <dbl> <dbl>
1 group1 x 0.06 -0.74 1.04
2 group1 y -0.36 -1.03 0.45
3 group2 x -0.04 -0.85 0.62
4 group2 y 0.06 -0.56 0.89
Can this summarise step be done without writing the quantile function 3 times.
I did a work around using this. But is there a nice way to do this.
df %>%
gather('variable','value', -group) %>%
group_by(group, variable) %>%
summarise(s = toString(round(quantile(value, c(0.25,0.5,0.75),na.rm = T),2))) %>%
separate(s, into = c('q25','median','q75'), sep = ',')
解决方案
You can nest
the data after the group_by
and then map
to quantile
df %>%
gather('variable','value', -group) %>%
group_by(group, variable) %>%
nest() %>%
mutate(quant = map(data, ~quantile(.$value, probs = c(0.25, 0.5, 0.75))),
quant = map(quant, t),
quant = map(quant, as.data.frame),
quant = map(quant, setNames, c("iqr25", "median", "iqr75")),
) %>%
unnest(quant) %>%
select(-data)
# A tibble: 4 x 5
group variable iqr25 median iqr75
<fct> <chr> <dbl> <dbl> <dbl>
1 group1 x -0.876 -0.173 0.471
2 group2 x -0.372 0.0507 0.519
3 group1 y -0.785 -0.109 0.618
4 group2 y -0.944 -0.117 0.647
这篇关于使用在dplyr中返回多个输出的函数将多个列添加到data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文