簡體   English   中英

在R中應用rowMeans()后,數據框的尺寸丟失

[英]Loss of dimensions of dataframe after applying rowMeans() in R

我對一個數據框進行了子集處理,並在其上應用了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()你正在創建一個vector出的dataframe 因此,您將從n行和k列轉到nx1向量。
對於n=8k=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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM