r - R中的值非零时返回列名-LMLPHP

我有提到的数据集。如何获得每个ID的列标题列表?

我尝试了以下操作:

'colnames<-'(t(apply(dat == 1, 1, function(x) c(`colnames`(dat)[x], rep(NA, 4-sum(x))))),
             paste("LearningA", 1:3))

res <- apply(df, 1, function(x) {
  out <- character(4) # create a 4-length vector of NAs
  tmp <- `colnames`(df)[which(x==1)] # store the column names in a tmp field
  `out`[1:length(tmp)] <- tmp # overwrite the relevant positions
  out
})

最佳答案

purrr的一个选项:

library(purrr)
df %>% split(.$ID) %>% map(~names(.x)[!!.x][-1])
# $`1`
# [1] "LearningA"
#
# $`2`
# [1] "LearningC"
#
# $`3`
# [1] "LearningA" "LearningB" "LearningC"
#
# $`4`
# [1] "LearningA" "LearningB"
#
# $`5`
# character(0)


df %>% split(.$ID) %>% map(~which(!!.x[-1]))
# $`1`
# [1] 1
#
# $`2`
# [1] 3
#
# $`3`
# [1] 1 2 3
#
# $`4`
# [1] 1 2
#
# $`5`
# integer(0)

您可能已经在评论中提及了以下内容:
library(tidyverse)
df %>% gather(,,-1) %>%
  group_by(ID,value) %>%
  summarize(key=paste(key,collapse=", ")) %>%
  spread(value,key)

# # A tibble: 5 x 5
# # Groups:   ID [5]
#        ID                             `0`                  `1`       `2`       `3`
#   * <int>                           <chr>                <chr>     <chr>     <chr>
#   1     1            LearningB, LearningC            LearningA      <NA>      <NA>
#   2     2            LearningA, LearningB                 <NA> LearningC      <NA>
#   3     3                            <NA>            LearningB LearningC LearningA
#   4     4                       LearningC LearningA, LearningB      <NA>      <NA>
#   5     5 LearningA, LearningB, LearningC                 <NA>      <NA>      <NA>

08-25 04:06