在 R 中融化数据框

Melting a dataframe in R

我有活跃用户的数据框,当用户在这个特定日期活跃时它是 1,当他不活跃时它是 0。
我正在尝试修改我的数据框:

df <- data.frame(userID=c("user123","user124","user125"),
                 `2021-03-01`=c(1,0,1),
                 `2021-03-08`=c(1,1,1),
                 `2021-03-15`=c(0,0,1))

以优雅的方式添加到此数据框:

df <- data.frame(userID=c("user123", "user125", "user123","user124","user125","user125"),
                 Active_WeekDate=c("2021-03-01","2021-03-01", "2021-03-08", "2021-03-08","2021-03-08","2021-03-15"))

我找到了这个解决方案,但还有更优雅的方法吗?

library(tidyr)
df <- pivot_longer(df, cols = -userID, names_to="Active_WeekDate")
df <- df[ df$value==1, ]
df <- df[ , c(1,2)]
df

非常感谢任何帮助!

我总是发现 dplyr 方法更优雅:

library(tidyverse)
df %>%
        pivot_longer(cols = -userID, names_to="Active_WeekDate") %>%
        filter(value == 1) %>%
        select(1:2) %>%
        mutate(Active_WeekDate = gsub("X", "", Active_WeekDate))

输出:

  userID  Active_WeekDate
  <chr>   <chr>          
1 user123 2021.03.01     
2 user123 2021.03.08     
3 user124 2021.03.08     
4 user125 2021.03.01     
5 user125 2021.03.08     
6 user125 2021.03.15 

这个呢?

df <- data.frame(userID=c("user123","user124","user125"),
                 `2021-03-01`=c(1,0,1),
                 `2021-03-08`=c(1,1,1),
                 `2021-03-15`=c(0,0,1))
library(tidyverse)
df %>% pivot_longer(starts_with('X'), names_to = 'Active_week_date',names_prefix = 'X', values_transform = list(value = as.logical)) %>%
  filter(value) %>% select(-value)
#> # A tibble: 6 x 2
#>   userID  Active_week_date
#>   <chr>   <chr>           
#> 1 user123 2021.03.01      
#> 2 user123 2021.03.08      
#> 3 user124 2021.03.08      
#> 4 user125 2021.03.01      
#> 5 user125 2021.03.08      
#> 6 user125 2021.03.15

reprex package (v2.0.0)

于 2021-06-04 创建