In the first set of code, you're using df.test[index.value]
, which returns a 1-column data frame:
str(df.test[index.value])
# 'data.frame': 5 obs. of 1 variable:
# $ v0: Factor w/ 3 levels "A","B","C": 1 2 3 2 3
Testing if a data frame is contained in some.list
will return FALSE
, since there are no matching data frames in some.list
.
On the other hand, df.test[[index.value]]
will return the vector corresponding to the column numbered with index.value
:
str(df.test[[index.value]])
# Factor w/ 3 levels "A","B","C": 1 2 3 2 3
As a result, your first segment of code will work with this indexing:
index.value = match('v0',names(df.test))
df.test2 <- df.test[df.test[[index.value]] %in% some.list, ]
df.test2
# v0 v1 v2 v3
# 2 B 2 6 15
# 3 C 3 7 54
# 4 B 4 8 75
# 5 C 12 34 45
However, there is a simpler syntax to grabbing a column from a data frame by name: df.test[["v0"]]
.