简体   繁体   English

用 pivot_wider() 替换 spread()

[英]Replacing spread() with pivot_wider()

I've got a dataset I'm working on and editing in R with the dplyr package.我有一个正在使用 dplyr 包在 R 中处理和编辑的数据集。 My code is:我的代码是:

    hiphop%>%
  mutate( sex = 
    case_when(
      sex == 1 ~ "female",
      sex == 0 ~ "male"
    )
  )%>%
  group_by(sex)%>%
  summarise_at(vars(intl,vocal,classical,folk,rock,country,pop,alternative,hiphop,unclassifiable),funs(mean))%>%
  pivot_longer(c(intl,vocal,classical,folk,rock,country,pop,alternative,hiphop,unclassifiable),names_to = "genre")%>%
spread(sex,value)%>%
  mutate(
    genredifference = abs(female-male)
  )%>%
  arrange(genredifference)%>%
  top_n(3)

Where I get this output:我在哪里得到这个输出:

Selecting by genredifference
# A tibble: 3 x 4
  genre   female  male genredifference
  <chr>    <dbl> <dbl>           <dbl>
1 country  0.786 0.392           0.394
2 vocal    0.880 1.57            0.688
3 rock     1.93  3.06            1.13 

I would like to get the same output but by replacing the spread() function with pivot_wider() (I believe that would be the one to be used).我想获得相同的输出,但将 spread() 函数替换为 pivot_wider() (我相信这将是要使用的那个)。 However, I cannot figure out how to do it.但是,我不知道该怎么做。

Thank you!谢谢!

PS: This is my dataset, in case you are interested: PS:这是我的数据集,如果你有兴趣的话:

hiphop <- read_csv("https://www.dropbox.com/s/5d8fwxrj3jtua1z/hiphop.csv?dl=1")

Based on the dropbox input data, some of the steps were already done.根据保管箱输入数据,一些步骤已经完成。 We can make some steps more compact by utilizing the select_helpers ie if we have a range of columns to select, use : , similarly in pivot_longer , we can also specify the columns not to be selected with - .我们可以通过使用select_helpers使一些步骤更紧凑,即如果我们有一系列的列可供选择,使用: ,类似地在pivot_longer中,我们也可以指定不选择的列- With pivot_wider , make sure to specify the arguments ( names_from , values_from ) as there are other arguments as well and without specifying arguments, it could match the arguments in the order of occurence使用pivot_wider时,请确保指定参数( names_fromvalues_from ),因为还有其他参数并且在不指定参数的情况下,它可以按出现顺序匹配参数

library(dplyr)
library(tidyr)
 hiphop %>%  
    group_by(sex)%>%
    summarise_at(vars(intl:unclassifiable), mean) %>%
    pivot_longer(cols = -sex) %>% 
    pivot_wider(names_from = sex, values_from = value) %>%
    mutate(genredifference = abs(Female-Male))%>%
    arrange(genredifference)%>%
    top_n(3)
# A tibble: 3 x 4
#  name    Female  Male genredifference
#  <chr>    <dbl> <dbl>           <dbl>
#1 country  0.786 0.392           0.394
#2 vocal    0.880 1.57            0.688
#3 rock     1.93  3.06            1.13 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM