繁体   English   中英

为什么 R 给我错误“dims [product 28550] do not match the length of object [1]”

[英]Why is R giving me the error “dims [product 28550] do not match the length of object [1]”

我是 R 的新手,所以请耐心等待。 我有 1 列的 dataframe (df),有 28550 个观察值。 基本上,这是随着时间的推移以 0.02 秒的时间分辨率进行的观察。 我想更改尺寸以获得 50 行和 571 列的 dataframe。 这将是每秒一列。 我检查它是否具有 dim 属性,如下所示。

>dim(df) 
[1] 28545     1

同时更改 dataframe 的尺寸如下:

>df1 <- dim(df) <- c(50, 571)

#I get the following error:

Error in dim(df) <- c(50, 571) : 
  dims [product 28550] do not match the length of object [1]

我似乎找不到发生这种情况的原因。 是不是数据类型不对,即integer? 观察数填满了571列,所以我不明白“不匹配对象的长度”的含义。

我也试过:

matrix(unlist(t(df)), byrow=T, 50, 571)

#I get the following error:

In matrix(unlist(t(df)), byrow = T, 50, 571) :
  data length [28545] is not a sub-multiple or multiple of the number of rows [50]

那么还有另一种方法可以做到这一点吗?

感谢帮助。

一种选择是unlist ,在末尾用length<-填充NA ,然后转换为matrix

matrix(`length<-`(unlist(df), 50 * 571), 50, 571)

即有一个可重复的例子

matrix(`length<-`(1:7, 10), 5, 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM