当R中的值非零时返回列名

时间:2018-03-24 23:18:36

标签: r

enter image description here

我有上面提到的数据集。如何获取每个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
})

2 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:0)

一种选择是使用apply

df <- data.frame(ID = 1:5, 
                 LearningA = c(1,0,3,1,0),
                 LearningB = c(0,0,1,1,0),
                 LearningC = c(0,2,2,0,0)
df                     )
#    ID LearningA LearningB LearningC
# 1  1         1         0         0
# 2  2         0         0         2
# 3  3         3         1         2
# 4  4         1         1         0
# 5  5         0         0         0

#Use apply (rowwise) to find columns having value > 0
apply(df[,2:ncol(df)], 1, function(x)which(x>0))


#ID-wise column header for each row in list
# [[1]]
# LearningA 
# 1 
# 
# [[2]]
# LearningC 
# 3 
# 
# [[3]]
# LearningA LearningB LearningC 
# 1         2         3 
# 
# [[4]]
# LearningA LearningB 
# 1         2 
# 
# [[5]]
# named integer(0)