What's the difference between [1], [1,], [,1],

2019-02-16 03:08发布

问题:

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?

回答1:

In R, operators are not used for one data type only. Operators can be overloaded for whatever data type you like (e.g. also S3/S4 classes).

In fact, that's the case for data.frames.

  • as data.frames are lists, the [i] and [[i]] (and $) show list-like behaviour.

  • row, colum indices do have an intuitive meaning for tables, and data.frames look like tables. Probably that is the reason why methods for data.frame [i, j] were defined.

You can even look at the definitions, they are coded in the S3 system (so methodname.class):

> `[.data.frame`

and

> `[[.data.frame`

(the backticks quote the function name, otherwise R would try to use the operator and end up with a syntax error)



标签: r statistics sas