R语言 具有多个Id和变量的dcast

goucqfw6  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(142)

我想"取消融化"一个有多个ID和多个融化变量的data.frame,但我被卡住了。
下面是我的数据框:

这是理想的结果

我试过:

unmelted <- dcast(setDT(melted), Id, Date, Type ~  Score, Time, 
                                value.var = c("Score","Time"), sep = "")

还有

unmelted <- melted %>%
   group_by(Id, Date, Type) %>%
   unite(variable, Score, Time)%>%
   spread(Score, Time, -Id, Date, Type)

我不能使用pivot_wider

x7yiwoj4

x7yiwoj41#

tidyverse解决方案,使用tidyr包中的gatherspread

library(dplyr)
library(tidyr) #version 1.0.0 which has pivot_wider

df1 %>% 
  group_by(Type) %>% 
  mutate(name_x = row_number()) %>% 
  gather(key=var, value=val, c(Score, Time)) %>% 
  mutate(var = paste(var, name_x, sep="_")) %>% 
  select(-name_x) %>% 
  spread(key=var, value=val)

#> # A tibble: 3 x 11
#> # Groups:   Type [3]
#>      id Date  Type  Score_1 Score_2 Score_3 Score_4 Time_1 Time_2 Time_3 Time_4
#>   <dbl> <chr> <chr>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  <chr>  <chr>  <chr> 
#> 1     1 2001~ aaa       123     456     789      NA 12:12  13:12  14:12  <NA>  
#> 2     2 2001~ ddd       113     145      NA      NA 15:12  16:12  <NA>   <NA>  
#> 3     3 2001~ bbb       789     145     113     145 17:12  18:12  19:12  20:12

您可以更方便地使用pivot_wider执行相同的操作:

df1 %>% 
  group_by(Type) %>% 
  mutate(name_x = row_number()) %>% 
  pivot_wider(id_cols = c("id","Date", "Type"), 
              names_from = c("name_x"), 
              values_from = c("Score", "Time"))

数据:

df1 <- data.frame(id=c(1,1,1,2,2,3,3,3,3),
                  Date = c(rep("2001-01-13", 3), rep("2001-01-16", 2), rep("2001-01-18", 4)),
                  Type = c(rep("aaa",3), rep("ddd", 2), rep("bbb",4)),
                  Score = c(123,456,789,113,145,789,145,113,145),
                  Time = paste0(12:20, ":12"),
                  stringsAsFactors = F)
kgsdhlau

kgsdhlau2#

我们可以用

library(data.table)
dcast(setDT(df1), id + Date + Type ~ rowid(id, Date, Type),
     value.var = c("Score","Time"), sep = "")
#   id       Date Type Score1 Score2 Score3 Score4 Time1 Time2 Time3 Time4
#1:  1 2001-01-13  aaa    123    456    789     NA 12:12 13:12 14:12  <NA>
#2:  2 2001-01-16  ddd    113    145     NA     NA 15:12 16:12  <NA>  <NA>
#3:  3 2001-01-18  bbb    789    145    113    145 17:12 18:12 19:12 20:12

使用来自@M的数据--

相关问题