从数据框中提取带有条件的列名 [英] Extracting column names with condition from a data frame
本文介绍了从数据框中提取带有条件的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dput(new)
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22), A1 = c(1, 1, 1, 1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), A2 = c(1,
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), A3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), A4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), A5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), A6 = c(0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), A7 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
), A8 = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1,
1, 1, 1, 0, 0), A9 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"))
我有以下数据框架。我需要提取并打印ID和逗号分隔的列名称(其中出现1)。例如:
I have the following data frame. I need to extract and print the id's and comma separated column names where 1 is appearing. For example:
1 A1,A2
2 A1,A2
3 A1
4 A1
6 A2,A8
7 A6,A8
等。 ..
如何进行?
这是我的尝试:
vec_ID <- c()
vec_JOB <- c()
job <- 0
for(i in 1 : length(ID)){
for(j in 2:10){
if(new[i,j]==1){
vec_ID[i] <- ID[i]
}
}
}
print(vec_ID)
vec_ID <- vec_ID[!is.na(vec_ID)]
#vec_ID <- as.data.frame(vec_ID)
print(vec_ID)
new_df <- new[ID[vec_ID],]
View(new_df)
for (i in 1:nrow(vec_ID)) {
}
推荐答案
这是使用 stack
和 aggregate $ c通过基R进行的一个班轮$ c>,
aggregate(ind ~ ID,
subset(cbind(ID = new$ID, stack(replace(new, new == 0, '')[-1])), values == 1),
toString)
这给出了
ID ind
1 1 A1, A2
2 2 A1, A2
3 3 A1
4 4 A1
5 6 A2, A8
6 7 A6, A8
7 8 A1, A8
8 9 A6, A8
9 10 A8
10 11 A1, A8
11 12 A6
12 13 A5, A8
13 15 A8
14 16 A8
15 17 A8
16 18 A8
17 19 A8
18 20 A8
19 21 A7
这篇关于从数据框中提取带有条件的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文