按时间和id r传播字符列

时间:2018-05-29 14:32:30

标签: r indexing spread

与许多其他人一样的问题,但仍然不同。 我经常看到人们想要一种方法将列扩展成几个,但它通常在df中,其中列中的每个名称都有一个度量值。

像这样:

head(df)
         id time   fish weight
        1  1    1 marlin      4
        2  1    1    cod      1
        3  1    2    cod      1
        4  2    1 salmon      2
        5  2    1    cod      2
        6  2    2    cod      3

所以我可以使用像这样的传播(或dcast或类似的传播:

df<-spread(df, fish,weight, fill=F)
   id time cod marlin salmon
1  1    1   1      4   <NA>
2  1    2   1   <NA>   <NA>
3  2    1   2   <NA>      2
4  2    2   3   <NA>   <NA>

但是,如果你没有变量的值(这里是重量),但只是想传播鱼类? 所以输出就像这样

  id time   Fish1      Fish2
   1    1   marlin    salmon
   1    2   cod         <NA>
   2    1   salmon       cod
   2    2   cod         <NA>
你怎么做的? 感谢您的任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:2)

我们需要按序列分组

df %>%
  select(-weight) %>%
  group_by(id, time) %>% 
  mutate(ind = paste0("Fish", row_number())) %>%
  spread(ind, fish)
# A tibble: 4 x 4
# Groups:   id, time [4]
#     id  time Fish1  Fish2
#  <int> <int> <chr>  <chr>
#1     1     1 marlin cod  
#2     1     2 cod    NA   
#3     2     1 salmon cod  
#4     2     2 cod    NA   

数据

df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L), time = c(1L, 1L, 
2L, 1L, 1L, 2L), fish = c("marlin", "cod", "cod", "salmon", "cod", 
"cod"), weight = c(4L, 1L, 1L, 2L, 2L, 3L)), .Names = c("id", 
"time", "fish", "weight"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))