简体   繁体   中英

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

I am new in R so bear with me. I have a dataframe (df) of 1 column with 28550 observations. Basically, this is an observation over time with 0.02 second time resolution. I would like to change the dimensions in order to have a dataframe of 50 rows and 571 columns. This would be one column per second. I check whether it has the dim attribute, which it does as below.

>dim(df) 
[1] 28545     1

While changing the dimensions of the dataframe as follows:

>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]

I cannot seem to find a reason for why this happens. Is it that it is the wrong type of data, ie integer? The number of observations fill 571 columns, so I do not understand the meaning of "do not match the length of object".

I have also tried:

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]

Is there then another way to do this?

Appreciate the help.

One option is to unlist , pad NA at the end with length<- and then convert to matrix

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

ie with a reproducible example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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