简体   繁体   中英

Getting subset of of data based on multiple column values

I am trying to remove rows based on whether or not columns 2 and 3 contain 0's. I keep getting very strange results. I tried to write it without subset initially because I read somewhere that subset should only be used for small amounts of data because of the memory cost. Neither attempt worked for me however. Can someone explain what I did wrong?

df <- data.frame(val1=c(1,2,3), val2=c(4,0,5), val3=c(3,0,6))
subset(df,df>0,c(2,3))
data.frame(df[df[,c(2,3)]!=0])

starting dataframe:

   val1   val2   val3
1  1       4       3
1  2       0       0
3  3       5       6

end goal:

   val1   val2   val3
1  1       4       3
3  3       5       6

I am trying to remove rows based on whether or not columns 2 and 3 contain 0's. I keep getting very strange results. I tried to write it without subset initially because I read somewhere that subset should only be used for small amounts of data because of the memory cost. Neither attempt worked for me however. Can someone explain what I did wrong?

df <- data.frame(val1=c(1,2,3), val2=c(4,0,5), val3=c(3,0,6))
subset(df,df>0,c(2,3))
data.frame(df[df[,c(2,3)]!=0])

starting dataframe:

   val1   val2   val3
1  1       4       3
1  2       0       0
3  3       5       6

end goal:

   val1   val2   val3
1  1       4       3
3  3       5       6

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