string - R - Can't access data frame column using same of column in a second data frame -


suppose have following 2 data frames:

a <- data.frame(1:4, c("one", "two", "three", "four")) b <- data.frame(1:4, c(4, 6, 7, 9), 4:7, c(9, 10, 44, 45))  names(b) <- c("one", "two", "three", "four") names(a) <- c("number", "group") 

given

a$"group" # "one" "two" "three" "four" b$"two" # 4 6 7 9 

why cant access b$"two" so?

b$(a$"group"[2]) # null 

due order of factor levels (a$group factor), transform a$group character vector

a$group <- as.character(a$group)

then following works:

b[, a$group[2]] b[[a$group[2]]] b[[a[["group"]][2]]] 

so, trick index columns via parantheses []


Comments