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
AdIan
  • 125
  • 1
  • 6

2 Answers2

1
> df[, a]
[1] -1.7170952  0.4502299 -2.5959374 -1.3582197
BellmanEqn
  • 791
  • 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,918
  • 2
  • 20
  • 39