R Error: Replacement has X Rows, Data has Y (2 Examples)

 

This tutorial explains how to handle the error message “replacement has X rows, data has Y” in the R programming language.

The tutorial consists of this information:

Let’s take a look at some R codes in action:

 

Constructing Example Data

The following data will be used as basement for this R programming language tutorial:

data <- data.frame(x1 = c(1, 1, 2, 3, 4),       # Create example data
                   x2 = "x")
data                                            # Print example data
#   x1 x2
# 1  1  x
# 2  1  x
# 3  2  x
# 4  3  x
# 5  4  x

As you can see based on the previous output of the RStudio console, the example data has five rows and two columns.

 

Example 1: Reproduce the Error: Replacement has X Rows, Data has Y

The following R syntax illustrates how to replicate the error message “replacement has X rows, data has Y”.

Let’s assume that we want to create a new data frame variable based on the values of the variable x1. Then, we might try to use the which function as shown in the following R code:

data$x1_range[which(data$x1 <= 2)] <- "<= 2"    # Try to create new variable
# Error in `$<-.data.frame`(`*tmp*`, x1_range, value = c("<= 2", "<= 2",  : 
#   replacement has 3 rows, data has 5

Unfortunately, the previous R syntax resulted in the error “replacement has X rows, data has Y”. The reason for that is that we didn’t create the new variable first, before we assigned values to it.

Let me explain…

 

Example 2: Fix the Error: Replacement has X Rows, Data has Y

This section shows how to solve the problems with the error message “replacement has X rows, data has Y”.

In order to avoid this error, we first have to append a new column to our data frame that contains only NA values:

data$x1_range <- NA                             # Initialize empty variable first

Now, we can apply exactly the same R code as we did in the previous example:

data$x1_range[which(data$x1 <= 2)] <- "<= 2"    # Fill in values
data                                            # Print updated data frame
#   x1 x2 x1_range
# 1  1  x     <= 2
# 2  1  x     <= 2
# 3  2  x     <= 2
# 4  3  x     <NA>
# 5  4  x     <NA>

This time it worked! We have created a new column with conditional values.

 

Video, Further Resources & Summary

I have recently released a video tutorial on my YouTube channel, which illustrates the R codes of the present tutorial. You can find the video below:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you could read the related articles of this homepage. I have released several articles already:

 

At this point you should know how to deal with the error message “replacement has X rows, data has Y” in the R programming language. If you have any further questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


24 Comments. Leave new

  • It doesn’t work, same error

    Reply
  • Hi Joachim,

    I’ve just seen this webpage&your post. I’m new to R bibliometrix. That’s the code: Error: replacement has 0 rows, data has 399. I’d be glad if you can help.

    Many thanx in advance

    Reply
  • Hi Joachim,

    I solved the problem, 🙂

    Thanx&Regards

    Erkan

    Reply
  • Hey Jaochim,
    Thanks for the video. I have been facing this error, and it doesnt seem to have gone away even after inititialising the variable. Please help? a bit urgent.
    this is the code –
    read_excel(path, col_names = TRUE)

    path <- as.data.frame(path)

    path$opinions <- NA

    path$opinions <- str_c(path$A, path$B)

    error – Error in `$<-.data.frame`(`*tmp*`, opinions, value = character(0)) :
    replacement has 0 rows, data has 1

    Reply
    • Hey Ananya,

      Thanks for the kind words, glad you have liked the video!

      Please look into this part of your code: str_c(path$A, path$B) It seems like it does not produce a valid output. Maybe you have misspelled the variable names?

      Regards,
      Joachim

      Reply
  • Hi Joachim,

    I am fairly new to R and struggling with this error, I don’t know how I would apply this solution to script. Could you please take a look? any help would be greatly appreciated, this is for my school assignment.
    https://gadm.org/download_country.html
    https://ec.europa.eu/eurostat/databrowser/view/demo_r_births/default/table?lang=en

    x = c(“ggmap”, “rgdal”, “rgeos”, “maptools”,
    “dplyr”, “tidyr”,”Rcpp”,”tmap”,”grid”,”png”, “readxl”,”sp”)
    install.packages(x)
    lapply(x, library , character.only = TRUE)
    gadm <- readRDS("gadm36_DEU_2_sp.rds")
    gadm@data$NAME_1
    dev.off()
    plot(gadm)
    germany <- read_excel("Birth.xlsx")
    gadm$NAME_1 %in% germany$GEO
    germany = rename(germany, NAME_1 = GEO)
    head(left_join(gadm@data , germany))
    gadm@data = left_join(gadm@data, germany)
    dev.off()
    qtm(gadm , "2011")

    Error in '$<- . data.frama'(*tmp*' , "geometry", value = list(list(list( : replacement has 403 rows, data has 493

    Reply
    • Hey Ruma,

      Thank you for the interesting question, and apologies for the delayed response, I just came back from vacation. Do you still need help with this?

      Regards,
      Joachim

      Reply
  • Hi , I have some problms, as follows,
    Error in `$<-.data.frame`(`*tmp*`, "OrderTPL", value = NA_character_)

    Reply
  • hello Joachim.

    I am fairly new to R and I need help, I want to plot multiple Netcdf files into one graph and I don’t know where to start.

    Reply
  • Sukrit Ghosh
    April 11, 2022 7:02 pm

    hello Joachim
    can you help me ?

    my codes are –
    train_poly <- as.data.frame(model.matrix(formula, data = X_train ))
    test_poly <- as.data.frame(model.matrix(formula, data = X_test ))
    train_poly$charges <- y_train
    test_poly$charges <- y_test

    its showing – Show in New Window
    Error in `$<-.data.frame`(`*tmp*`, charges, value = c(16884.924, 1725.5523, :
    replacement has 935 rows, data has 0

    Reply
    • Hey Sukrit,

      It seems like one of your data frames doesn’t contain any values. What is returned when you apply this code?

      dim(train_poly)
       
      dim(test_poly)

      Regards,
      Joachim

      Reply
  • Hi Joachim!

    Is there a possibility to overwrite a certain column with the which-function and prevent this error?
    e.g.:
    df$X22test.p <- replace(df$X22test.p, which(X22test.p < 0), NA)

    BR, Lisa

    Reply
  • JOVIN VICENT
    June 20, 2022 9:17 am

    Hello Joachim

    I was trying to use the random forest to predict crop yield on a raster stack datasets but I am getting this error;

    Error in ‘[[<-.data.frame'('*tmp*,name,value=c('2506'=5350.63592071787, :replacement has 8740576 rows, data has 9469714

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top