gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:41:00 25 4
gpt4 key购买 nike

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
})

最佳答案

带有 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>

关于当 R 中的值非零时返回列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com