Read the last row and remove the column based on a condition in R -


my data frame looks this:

   var1   var2 1   1       11 2   2       na 3   na      na 4   4       14 5   na      na 6   6       16 7   7       17 8   8       na 9   9       19 10  10      na 11   1       0 

i need read last row( arbitrary) of data frame , if 'true', need retain column , if 'false' need delete particular column , store output in new data frame

for above example result should be:

   var1 1   1 2   2 3   na 4   4 5   na 6   6 7   7 8   8 9   9 10  10 

2nd column removed, since last row reads '0'.

try using as.logical:

> mydf[as.logical(mydf[nrow(mydf), ])]    var1 1     1 2     2 3    na 4     4 5    na 6     6 7     7 8     8 9     9 10   10 11    1 

or mydf[, as.logical(mydf[nrow(mydf), ]), drop = false] if want same code compatible matrices too.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -