简体   繁体   中英

how to delete some columns based on values in last row in R

I have data frame like this

    1112 1232 1233 1233 1232 1111 0000
    1111 1111 0000 1211 0001 1110 1212
    1212 1111 1111 1111 1111 1111 1111
    1111 1111 1111 1111 1111 1111 1111
    20 121 12 50 12 11 9

I want to delete those columns based on values of the last row. as an example if values in last row are either bigger than 20 or smaller than 10 , the whole column to be deleted and the output be like this

1112 1233 1232 1111 
1111 0000 0001 1110 
1212 1111 1111 1111 
1111 1111 1111 1111 
20 12 12 11 

any recommend would be appreciated..

We extract the last row ( tail(df1,1) ), unlist it to create a vector ('v1'), then based on the logical condition , we subset the columns of the dataset.

 v1 <-unlist(tail(df1,1))
 df1[!(v1 > 20 | v1<10)]
 #    v1   v3   v5   v6
 #1 1112 1233 1232 1111
 #2 1111    0    1 1110
 #3 1212 1111 1111 1111
 #4 1111 1111 1111 1111
 #5   20   12   12   11

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