Error : replacement has x rows, data has y

I am running this R script from Java (I am using Renjin):

getCoefficients <- function(x, y, Regions) {
  print(class(x)) #"matrix"
  print(dim(x))   #8  456
  print(class(y))  #"numeric"
  print(length(y)) #456
  print(Regions)  #"r1"      "r2"     "r3"
  nbRegions <- length(Regions)
  lengthY <- length(y)
  colRegions <- NULL
  from <- 1
  to <- lengthY / nbRegions
  for (i in 1:nbRegions) {
    region <- Regions[i]
    c <- cbind(region, from:to)
    colRegions <- rbind(colRegions, c)
  indexCols <-
  print(class(indexCols)) #"data.frame"
  print(dim(indexCols))   #456   2
  x_ <- t(x) 
  print(class(x_))  #"matrix"
  print(dim(x_))    #456   8

  data_ <- cbind(indexCols, y, x_)
  print(class(data_)) #"data.frame"
  print(dim(data_))   #456  11
  dataFrame <- pdata.frame(data_)
  print(class(dataFrame)) #"pdata.frame" "data.frame" 
  print(dim(dataFrame))  #456  11
  colnames(dataFrame) <- c("region", "date", "y", "a", "b", "c", "d", "e", "f", "r", "o")
  model_RE_S <- try(plm(y ~ x_, data = dataFrame, model = "random", index = c("region", "date"), effect = "twoways")) #Error : replacement has 3648 rows, data has 456  :rotating_light:

  summryModel <- summary(model_RE_S)
  coeff <- as.numeric(summryModel$coefficients)


I am getting the following error and I have no idea how to resolve it:

Error : replacement has 3648 rows, data has 456

The dimensions of the data I am using are:

  • 3 regions (r1, r2, r3)
  • 8 variables
  • 456 observations (3 regions x 152 weeks)
  • y is the dependent variable (456 data points for all 3 regions)
  • x is the matrix with the data of the 8 dependent variables so its dimensions are (456 columns and 8 rows), and x_ (its transpose) is what will be used in the modelling.
    a sample of x looks like:

a sample of x_ the transpose of x :

a sample of data_ :

a sample of dataFrame :
thank you!


Could you provide more info on the x, y and z variable. What data type are they (all matrices?) and if matrix, what are the dimensions.

Use the class() and dim() functions to help you out if needed. e.g. dim(x) or class(x)


I have edited the topic with the requested details. thank you!


I still like to know the data type (e.g. list, matrix, data.frame) of each. Please use the class() function to get that info for x, y and Regions.

Also, try and run the inside of the function line by line to see where the error occurs. You can do this by declaring the attributes above the function, then run the inside line by line.


#This is how the function is called in your script
getCoefficients(var1, var2, var3)

#So run the code up until there then stop and assign the variables to the ones used within the function
x = var1
y = var2
Regions = var3

#Now run the inside of the function line by line (not the whole function at once!) and see when the error occurs.


Hello PJ, kindly check the edited topic again. i hope that is what you you asked for.
do you please have any explanation why the content in column "V2" is not ordered anymore once the dataframe is converted to a pdataframe?
thank you in advance!


After playing around with your code (I don't have real data of course), I think the error lies within the formula you write in the plm funciton. the formula should reference variables present in the data frame, and x_ is another data frame so that wouldn't work I think. Why don't you try this formula:

model_RE_S <- plm(y ~ a + b + c + d + e + f + r + o, data = dataFrame, 
                    model = "random", index = c("region", "date"), effect = "twoways")

I get another error there, but I think that is because I generate random data.

Does this help?

unfortunately not :confused:

Error: duplicate couples (time-id)


This probably means you have duplicates in your region and date column, which is not allowed I think.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.