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

当R中的值非零时返回列名,r,R,我有提到的数据集。如何获取每个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 ve

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

'colnames一个选项是使用
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)
df0
应用(df[,2:ncol(df)],1,函数(x)哪个(x>0))
#列表中每行的ID列标题
# [[1]]
#学习A
# 1 
# 
# [[2]]
#学习C
# 3 
# 
# [[3]]
#学习A学习B学习C
# 1         2         3 
# 
# [[4]]
#学习A学习B
# 1         2 
# 
# [[5]]
#命名整数(0)

带有
purr
的选项:

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>
库(tidyverse)
df%%>%聚集(,-1)%%>%
分组依据(ID,值)%>%
汇总(键=粘贴(键,折叠“”,“”)%>%
排列(值、键)
##tibble:5 x 5
##组:ID[5]
#ID`0``1``2``3`
#   *                                                      
#1学习B,学习C学习A
#2.学习A、学习B、学习C
#3.学习B.学习C.学习A
#学习C学习A,学习B
#学习A,学习B,学习C

选择列时,哪些值必须为非零?@MaxFt例如,在第3行中,预期输出应有一个包含3个值的列表LearningA、LearningB、LearningC。或者,1、2和3可以是列,对于第3行,LearningA应该列在3下面,依此类推……谢谢