0

As we know, it is each to subset a column by a $ sign and the name of a column.

df <- data.frame(matrix(rnorm(12),4,3))
df$X1

what if I want to subset a column with a variable?

a="X1"
df$a
AndyIan
  • 115
  • 6

2 Answers2

1
> df[, a]
[1] -1.7170952  0.4502299 -2.5959374 -1.3582197
BellmanEqn
  • 773
  • 3
  • 11
0

You can have the same output with this format:

> df$X1
[1]  0.1591573  1.3328099 -0.2382000 -0.7364309
> df[,a]
[1]  0.1591573  1.3328099 -0.2382000 -0.7364309
Terru_theTerror
  • 4,788
  • 2
  • 17
  • 37