df$x
and df[[x]]
do the same thing.
Let's assume that you have a data set named one
. One of these variables is a factor variable, Region
. Using one$Region
will allow you to select a specific variable. Consider the following:
one <- read.csv("IED.csv")
one$Region
Running the following code also allows you to isolate that variable/level.
one[["Region"]]
Each code produces the following output:
> one$Region
[1] RC SOUTH RC SOUTH RC SOUTH RC EAST RC EAST
[6] RC EAST RC EAST RC EAST RC EAST RC EAST
[11] RC SOUTH RC SOUTH RC EAST RC EAST RC EAST
[16] RC EAST RC EAST RC SOUTH RC SOUTH RC EAST
[21] RC SOUTH RC EAST RC CAPITAL RC EAST RC EAST
> one[["Region"]]
[1] RC SOUTH RC SOUTH RC SOUTH RC EAST RC EAST
[6] RC EAST RC EAST RC EAST RC EAST RC EAST
[11] RC SOUTH RC SOUTH RC EAST RC EAST RC EAST
[16] RC EAST RC EAST RC SOUTH RC SOUTH RC EAST
[21] RC SOUTH RC EAST RC CAPITAL RC EAST RC EAST
"They both return the "same" results, but not necessarily in the same format." - I didn't notice any differences. Each command produced the same outputs in the same format. Perhaps its your data.
Hope that helps.
EDIT:
Misread the original question. df["x"]
produces the following:
> one["Region"]
Region
1 RC SOUTH
2 RC SOUTH
3 RC SOUTH
4 RC EAST
5 RC EAST
6 RC EAST
7 RC EAST
8 RC EAST
9 RC EAST
10 RC EAST
Not sure why the difference occurs.