I was performing some simple subsetting on my data when I tried to remove all rows with a negative value.
Example code:
df1 <- df1[df1$var2 >= 0,]
In my current dataset this action should remove four rows. However, no rows are removed after executing.
Strangely, the following code does only leaves those four rows that should be removed by the first code:
df1 <- df1[df1$var2 < 0,]
I found that using subset(df1, var2 >= 0)
does work and removes the four rows that I want to have removed. But I always thought that the first code was the same as using subset()
? Does someone know why the very first code doesn't work the way I intend it to?
Edit, including some data from my dataset:
> dput(df1[1:10,215:220])
structure(list(KBUY_CER = c(3L, 0L, 0L, 0L, 0L, 3L, 2L, 0L, 3L,
2L), KBUY_PRO = c(1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L), KBUY_DEF = c(1L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L), THINK_SP = c(NA, 1L, NA,
NA, NA, NA, NA, 1L, 1L, NA), dwifexp = c(NA, 0L, NA, NA, NA,
NA, NA, 0L, 0L, NA), dwifdoll = c(NA, 1500L, NA, NA, NA, NA,
NA, 600L, 600L, NA)), row.names = c("389", "390", "391", "392",
"393", "394", "395", "396", "397", "398"), class = "data.frame")
All columns I've selected data from work as intented, only column 215 (KBUY_CER
) seems to have this problem.
I just found out that the four rows that should be removed are actually not negative but NA
values, which explains why both [
's don't remove those rows from the selection. But does subset()
always remove NA
values then?