使用dplyr更好的输出-打破功能和结果 [英] Better output with dplyr -- breaking functions and results

查看:72
本文介绍了使用dplyr更好的输出-打破功能和结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个长期存在的问题,但是现在我真的可以解决这个难题了。我一直在使用dplyr,我认为总结变量很棒。但是,我试图显示仅部分成功的数据透视表。 Dplyr总是报告包含所有结果的单行,这很烦人。我必须将结果复制粘贴以擅长组织所有内容...



我得到了代码



应类似于以下内容:



因为我总是使用这种样式报告结果



使用此代码可获得相同的结果:

  library(tidyverse) 
set.seed(123)
ds<-data.frame(group = c( american, canadian),
iq = rnorm(n = 50,mean = 100, sd = 15),
收入= rnorm(n = 50,平均值= 1500,sd = 300),
math = rnorm(n = 50,平均值= 5,sd = 2))
ds%>%
group_by(group)%>%
summarise_at(vars(iq,income,math),funs(mean,sd))%>%
t%> ;%
as.data.frame%>%
rownames_to_column%&%;%
split(rowname,into = c( feature, fun),sep = _ )

为了澄清,我已经,但尝试了这种代码,但传播仅适用于一个汇总(均值或sd等)。某些人使用collect(),但使用group_by和collect()则很复杂。



感谢您的帮助。

解决方案

在总结后,代替转置( t )和更改类类型步骤,执行 gather 将其更改为 long格式,然后 spread 使用分开 unite

进行一些修改

  library(tidyverse)
ds%>%
group_by(group)%>%
summarise_at(vars(iq,income,math),funs(mean ,sd))%>%
collect(key,val,iq_mean:math_sd)%&%;%
split(key,into = c('key1','key2'))%> %
unite(组,组,键2)%>%
点差(组,val)


This is a long-lasting question, but now I really to solve this puzzle. I'm using dplyr all the time and I think it is great to summarise variables. However, I'm trying to display a pivot table with partial success only. Dplyr always reports one single row with all results, what's annoying. I have to copy-paste the results to excel to organize everything...

I got the code here and it almost working.

This result

Should be like the following one:

Because I always report my results using this style

Use this code to get the same results:

library(tidyverse) 
set.seed(123)
ds <- data.frame(group=c("american", "canadian"), 
                 iq=rnorm(n=50,mean=100,sd=15),
                 income=rnorm(n=50, mean=1500, sd=300),
                 math=rnorm(n=50, mean=5, sd=2))
ds %>% 
  group_by(group) %>% 
   summarise_at(vars(iq, income, math),funs(mean, sd)) %>%
  t %>% 
  as.data.frame %>% 
  rownames_to_column %>%
  separate(rowname, into = c("feature", "fun"), sep = "_")

To clarify, I've tried this code, but spread works with only one summary (mean or sd, etc). Some people use gather(), but it's complicated to work with group_by and gather().

Thanks for any help.

解决方案

Instead of transposing (t) and changing the class types, after the summarise step, do a gather to change it to 'long' format and then spread it back after doing some modifications with separate and unite

library(tidyverse)
ds %>% 
  group_by(group) %>% 
  summarise_at(vars(iq, income, math),funs(mean, sd)) %>% 
  gather(key, val, iq_mean:math_sd) %>% 
  separate(key, into = c('key1', 'key2')) %>% 
  unite(group, group, key2) %>%
  spread(group, val)

这篇关于使用dplyr更好的输出-打破功能和结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆