如何创建新列并通过 r 中的选定行添加列名

How create new column an add column names by selected row in r

a<-c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE)
b<-c(TRUE,FALSE,TRUE,FALSE,FALSE,FALSE)
c<-c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE)
costumer<-c("one","two","three","four","five","six")
df<-data.frame(costumer,a,b,c)

这是示例代码。看起来像这样打印:

  costumer     a     b     c 
1      one  TRUE  TRUE  TRUE  
2      two FALSE FALSE  TRUE 
3    three  TRUE  TRUE  TRUE 
4     four FALSE FALSE FALSE 
5     five  TRUE FALSE  TRUE 
6      six FALSE FALSE FALSE

我想创建一个新列 df$items,它只包含数据中每一行的列名 TRUE。像这样:

  costumer     a     b     c items
1      one  TRUE  TRUE  TRUE  a,b,c
2      two FALSE FALSE  TRUE  c
3    three  TRUE  TRUE  TRUE  a,b,c
4     four FALSE FALSE FALSE 
5     five  TRUE FALSE  TRUE 
6      six FALSE FALSE FALSE

我想过用apply函数或者用which来选择索引,但是想不通。谁能帮帮我?

df$items = apply(df[2:4], 1, function(x) toString(names(df[2:4])[x]))
df
#   custumer     a     b     c   items
# 1      one  TRUE  TRUE  TRUE a, b, c
# 2      two FALSE FALSE  TRUE       c
# 3    three  TRUE  TRUE  TRUE a, b, c
# 4     four FALSE FALSE FALSE        
# 5     five  TRUE FALSE  TRUE    a, c
# 6      six FALSE FALSE FALSE       

你可以使用

df$items <- apply(df, 1, function(x) toString(names(df)[which(x == TRUE)]))

输出

#   custumer     a     b     c   items
# 1      one  TRUE  TRUE  TRUE a, b, c
# 2      two FALSE FALSE  TRUE       c
# 3    three  TRUE  TRUE  TRUE a, b, c
# 4     four FALSE FALSE FALSE        
# 5     five  TRUE FALSE  TRUE    a, c
# 6      six FALSE FALSE FALSE        
df$items <- apply(df, 1, function(x) paste0(names(df)[x == TRUE], collapse = ","))
df
  custumer     a     b     c items
1      one  TRUE  TRUE  TRUE a,b,c
2      two FALSE FALSE  TRUE     c
3    three  TRUE  TRUE  TRUE a,b,c
4     four FALSE FALSE FALSE      
5     five  TRUE FALSE  TRUE   a,c
6      six FALSE FALSE FALSE      

我们可以使用pivot_longer重塑为'long'格式,然后按paste

分组
library(dplyr)
library(tidyr)
library(stringr)
df %>%
    pivot_longer(cols = a:c) %>%
    group_by(costumer) %>% 
    summarise(items = toString(name[value])) %>% 
    left_join(df)