[英]select multiple column that meet a condition
I have a data frame and I want to select all rows that meet a particular condition as for example !=0. 我有一个数据框,我想选择满足特定条件的所有行,例如!= 0。 I can do it for each column but it make a real long line.
我可以为每一列做到这一点,但它确实很长。
df.Individual <- df.category[df.category[,10]!=0 & df.category[,9]!=0 ........ & df.category[,2]!=0, ][,1]
I would like to select a group of columns something like this, but I can not figure how! 我想选择一组类似这样的列,但是我不知道怎么做!
df.category[df.category[,c(10:5)]!=0 & c(6:2)]>0 ][,1]
Thanks! 谢谢!
structure(list(Individual = structure(1L, .Label = c("aaa"), class = "factor"), `Class1` = 1L,
`Class2` = 0L, `Class3` = 1L, `Class4 ` = 2L, `Class5` = 3L, `Class6` = 1L, Class7 = 1L, Class8 = 1L, Class9 = 1L), .Names = c("Individual",
"Class1", "Class2", "Class3", "Class4", "Class5", "Class6", "Class7", "Class8", "Class9"), row.names = 2L, class = "data.frame")
Edit: 编辑:
I need to get all the column combination. 我需要获取所有列组合。 something like a for cycle.
类似于for循环。 I want to have a list of Individual sorted for their class to use as factor levels on the y axis of ggplot
我想为他们的班级排序一个个人列表,以用作ggplot y轴上的因子水平
as an example. 举个例子。 but here are just some combination listed and I want all the possible column combination.
但是这里只是列出了一些组合,我希望所有可能的列组合。
df.Individual.1 <- df.category[ df.category[,10]!=0 &
df.category[,9]!=0 &
df.category[,8]!=0 ,] [,1]
df.Individual.2 <- df.category[ df.category[,10]!=0 &
df.category[,9]!=0 &
df.category[,8]<=0 ,] [,1]
df.Individual.3 <- df.category[ df.category[,10]!=0 &
df.category[,9]<=0 &
df.category[,8]!=0 ,] [,1]
df.Individual.4 <- df.category[ df.category[,10]!=0 &
df.category[,9]<=0 &
df.category[,8]<=0 ,] [,1]
unlist(list(df.Individual.1,df.Individual.2,df.Individual.3,df.Individual.4))
At the end I need a list with individual sorted for their class status. 最后,我需要一个列表,其中包含针对其课程状态进行排序的个人。 First All Class positiv, than the first class is positiv and the other are one each negative.
First All Class positiv,而不是头等舱positiv,另一个都是负数。
1 1 1
1 1 0
1 0 1
1 0 0
0 1 1
0 1 0
0 0 1
0 0 0
here an example for 3 columns. 这是3列的示例。 Thanks!
谢谢!
I would use rowSums
(much faster than an apply
loop). 我会用
rowSums
(比快得多apply
循环)。 Here is a logical vector of rows where columns 5
through 10
only have non-zeroes: 这是行的逻辑向量,其中第
5
到第10
列仅具有非零值:
rowSums(df.category[,c(5:10)] != 0) == (10-5+1)
or better: 或更好:
rowSums(df.category[,c(5:10)] == 0) == 0
You can combine such logical vectors using &
, then use that to extract from df.category: 您可以使用
&
组合这些逻辑向量,然后使用来从df.category中提取:
logical1 <- rowSums(df.category[,c(5:10)] == 0) == 0
logical2 <- rowSums(df.category[,c( 2:6)] <= 0) == 0
df.category[logical1 & logical2, ]
Edit: Your updated question is a lot more vague, maybe try something like this: 编辑:您更新的问题更加模糊,也许尝试这样的事情:
df <- df.category
classes.col <- grep("Class", colnames(df), value = TRUE)
df$Attended <- apply(df[classes.col] > 0, 1, paste, sep = "_")
split(df$Individual, df$Attended)
一种可能性:
df[apply(df[,-1]!=0,1,all),]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.