Reputation: 45
My data frame looks like 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 to read the last row( arbitrary) of a data frame and if it is 'TRUE', I need to retain that column and if it is 'FALSE' I need to delete that particular column and store the output in a new data frame
For the above example the 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 is removed, since the last row reads '0'.
Upvotes: 1
Views: 139
Reputation: 193517
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 you want the same code compatible with matrices too.
Upvotes: 2