Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R_inferno.pdf
Скачиваний:
11
Добавлен:
21.05.2015
Размер:
947.8 Кб
Скачать

8.2. CHIMERAS

CIRCLE 8. BELIEVING IT DOES AS INTENDED

When the call to data.frame uses a tag (name) for an item, it is expected that the corresponding column of the output will have that name. However, column names that are already there take precedence.

Notice also that the data frames contain a factor column rather than character.

8.2.42cbind favors matrices

If you cbind two matrices, you get a matrix. If you cbind two data frames, you get a data frame. If you cbind two vectors, you get a matrix:

> is.matrix(cbind(x=1:10, y=rnorm(10))) [1] TRUE

If you want a data frame, then use the data.frame function:

> dfxy <- data.frame(x=1:10, y=rnorm(10))

8.2.43data frame equal number of rows

A data frame is implemented as a list. But not just any list will do|each component must represent the same number of rows. If you work hard enough, you might be able to produce a data frame that breaks the rule. More likely your frustration will be that R stops you from getting to such an absurd result.

8.2.44matrices in data frames

Let's make two data frames:

>ymat <- array(1:6, c(3,2))

>xdf6 <- data.frame(X=101:103, Y=ymat)

>xdf7 <- data.frame(X=101:103)

>xdf7$Y <- ymat

>xdf6

X Y.1 Y.2

1 101

1

4

2 102

2

5

3 103

3

6

> xdf7

 

 

X Y.1 Y.2

1 101

1

4

2 102

2

5

3 103

3

6

>dim(xdf6) [1] 3 3

>dim(xdf7) [1] 3 2

100

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]