简体   繁体   中英

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. 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. I want to have a list of Individual sorted for their class to use as factor levels on the y axis of ggplot

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.

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. Thanks!

I would use rowSums (much faster than an apply loop). Here is a logical vector of rows where columns 5 through 10 only have non-zeroes:

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:

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),]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM