Possible Duplicate:
In R, what is the difference between the [] and [[]] notations for accessing the elements of a list?
I'm confused with the difference of [1], [1,], [,1], [[1]] for dataframe type.
As I know, [1,] will fetch the first row of a matrix
, [,1] will fetch the first column. [[1]] will fetch the first element of a list
.
But I checked the document of data.frame
, which says
A data frame is a list of variables of the same number of rows with unique row names
Then I typed in some code to test the usage.
>L3 <- LETTERS[1:3]
>(d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, replace=TRUE)))
x y fac
1 1 1 C
2 1 2 B
3 1 3 C
4 1 4 C
5 1 5 A
6 1 6 B
7 1 7 C
8 1 8 A
9 1 9 A
10 1 10 A
> d[1]
x
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
>d[1,]
x y fac
1 1 1 C
>d[,1]
[1] 1 1 1 1 1 1 1 1 1 1
>d[[1]]
[1] 1 1 1 1 1 1 1 1 1 1
What confused me is: [1,] and [,1] is only used in matrix
. [[1]] is only used in list
, and [1] is used in vector
, but why all of them are available in dataframe?
Could anybody explain the difference of these usage?