i子集一个dataframe,我在其上应用了rowmeans((,但是丢失了结果变量('y'(的尺寸,我无法在我的进一步中使用'y'代码。
dim(mtcars)
# [1] 32 11
y = rowMeans((mtcars[,3:6]))
dim(y)
# NULL
为什么'y'不再是数据框架?我该怎么做才能恢复其尺寸?我尝试了以下操作,但没有起作用。
as.data.frame(y)
# or
data.frame(y)
应用rowMeans()
时,您是在dataframe
中创建vector
。因此,您要从 n 行和 k 列到 nx1 vector。
对于n=8
和k=5
的情况,我们将有:
> a=as.data.frame(matrix(1:40,8,5))
> a
V1 V2 V3 V4 V5
1 1 9 17 25 33
2 2 10 18 26 34
3 3 11 19 27 35
4 4 12 20 28 36
5 5 13 21 29 37
6 6 14 22 30 38
7 7 15 23 31 39
8 8 16 24 32 40
> rowMeans(a)
[1] 17 18 19 20 21 22 23 24