Question

I'm having some issues with the following:

b <- some.data.frame
index.value = match(someCol,names(b)
#Head will return nothing:
c <- b[b[index.value] %in% some.list, ]
head(c)
#Head will return values:
c <- b[b$someCol %in% some.list, ]
head(c)

Head will return 0 rows for me. But if I use the name of the column it will work. What gives?

EDIT:

Here is an example:

v0 <- c('A','B','C','B', 'C')
v1 <- c(1,2,3,4,12)
v2 <- c(5,6,7,8,34)
v3 <- c(10,15,54,75,45)

df.test <- data.frame(v0,v1,v2,v3)
some.list = c('B', 'C')

#Does not return values
index.value = match('v0',names(df.test))
df.test2 <- df.test[df.test[index.value] %in% some.list,  ] 
df.test2

#Return Values
df.test2 <- df.test[df.test$v0 %in% some.list,  ] 
df.test2
Was it helpful?

Solution

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"]].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top