Reputation: 1277
Suppose that I have a vector x
whose elements I want to use to extract columns from a matrix or data frame M
.
If x[1] = "A"
, I cannot use M$x[1]
to extract the column with header name A
, because M$A
is recognized while M$"A"
is not. How can I remove the quotes so that M$x[1]
is M$A
rather than M$"A"
in this instance?
Upvotes: 1
Views: 526
Reputation: 193497
Don't use $
in this case; use [
instead. Here's a minimal example (if I understand what you're trying to do).
mydf <- data.frame(A = 1:2, B = 3:4)
mydf
# A B
# 1 1 3
# 2 2 4
x <- c("A", "B")
x
# [1] "A" "B"
mydf[, x[1]] ## As a vector
# [1] 1 2
mydf[, x[1], drop = FALSE] ## As a single column `data.frame`
# A
# 1 1
# 2 2
I think you would find your answer in the R Inferno. Start around Circle 8: "Believing it does as intended", one of the "string not the name" sub-sections.... You might also find some explanation in the line The main difference is that $ does not allow computed indices, whereas [[ does.
from the help page at ?Extract
.
Note that this approach is taken because the question specified using the approach to extract columns from a matrix or data frame, in which case, the [row, column]
mode of extraction is really the way to go anyway (and the $
approach would not work with a matrix
).
Upvotes: 2