Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering of dataframe columns displaying a counter intuitive behavior (R)

Take as an example the dataframe below. I need to change the dataframe by keeping only the columns that are in the filter objects.

test <- data.frame(A = c(1,6,1,2,3) , B = c(1,2,1,1,2), C = c(1,7,6,4,1), D = c(1,1,1,1,1))
filter <- c("A", "B", "C", "D")
filter2 <- c("A","B","D")

To do that I’m using this piece of code:

`%ni%` <- Negate(`%in%`)
test <- test[,-which(names(test) %ni% filter2)]

If I use the filter2 object I get what is expected:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  A B D
1 1 1 1
2 6 2 1
3 1 1 1
4 2 1 1
5 3 2 1

However, if I use the filter object, I get a dataframe with zero columns:

data frame with 0 columns and 5 rows

I expected that I would get an untouched dataframe, since filter had all test columns in it. Why does this happen and how can I write a more reliable code to not get empty dataframes in these situations?

>Solution :

Use ! instead of -

test[,!(names(test) %ni% filter2)]
test[,!(names(test) %ni% filter)]

by wrapping with which and using -, it works only when the length of output of which is greater than 0

> which(names(test) %ni% filter2)
[1] 3
> which(names(test) %ni% filter)
integer(0)

By doing the -, there is no change in the integer(0) case

> -which(names(test) %ni% filter)
integer(0)
> -which(names(test) %ni% filter2)
[1] -3

thus,

> test[integer(0)]
data frame with 0 columns and 5 rows
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading