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:



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.

41 Comments. Leave new

  • It doesn’t work, same error

  • 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

    • Hi Erkan,

      Could you please share the code you have used? 🙂



      • L <- read.table(choose.files(), header = T)
        L$date <-as.Date(L$date, "%m/%d/%Y")
        and it shows error like this
        Error in `$<-.data.frame`(`*tmp*`, date, value = numeric(0)) :
        replacement has 0 rows, data has 348

        • Hi Ayesha,

          This indicates that your data set L was not imported properly. What is returned when you try to print the first six rows of your data?

          Try to execute the following code:



        • K. Silambarasan
          October 30, 2023 2:29 pm

          How it resolve the error?

          Error in `$<-.data.frame`(`*tmp*`, qres.Mod1, value = c(`1` = 0.615453934552787, :
          replacement has 263 rows, data has 0

          • Hello!

            The error you’re seeing is caused by trying to assign a vector (or another data frame) with 263 rows to a column in a data frame that has 0 rows. In R, when you’re trying to assign values to a column in a data frame, the number of rows in the column must match the number of rows in the data frame.

            First, ensure that the data frame you’re trying to modify is correctly loaded or initialized. Maybe it’s empty when it shouldn’t be. Check the number of rows as follows. If it returns 0, then that’s the root of the issue.


            If you intended to add this column to an existing data frame with rows, ensure that the number of rows in the vector/data frame you’re trying to assign matches the number of rows in the main data frame.


            I hope one of these checks help you to determine the cause of the error.


  • Hi Joachim,

    I solved the problem, 🙂



  • 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

    • 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?


  • 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.

    x = c(“ggmap”, “rgdal”, “rgeos”, “maptools”,
    “dplyr”, “tidyr”,”Rcpp”,”tmap”,”grid”,”png”, “readxl”,”sp”)
    lapply(x, library , character.only = TRUE)
    gadm <- readRDS("gadm36_DEU_2_sp.rds")
    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)
    qtm(gadm , "2011")

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

    • 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?


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

  • 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.

  • 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

    • Hey Sukrit,

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



  • Hi Joachim!

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

    BR, Lisa

    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

  • can u make video tutorial on fable time series forecasting? As I am unable to run the codes related to fable. There is a problem with the data frame although I am using tsibble no progress. I have 2 variable date and reports. I use the date as an index and reports as key. But then some data become lose. I don’t know where is problem. Can u help me?

    • Hi Ayesha,

      I’m not an expert on fable time series forecasting, however, thank you for the topic suggestion! I’ll keep this in mind for future tutorials.


  • MainQuery$UnvalidShp <- ifelse(grepl('Unmatched', MainQuery$MatchType),0,1)

    Error in `$<-.data.frame`(`*tmp*`, UnvalidShp, value = logical(0)) :
    replacement has 0 rows, data has 856248

    I'm facing this error

    • Hi Apexx,

      Could you please illustrate the structure of your data? What is returned when you execute the following line of code?



      • Sure,

        So main query is my data frame,
        I want to flag 0 if my match type column has “Matched” and 1 if it has Unmatched.
        My grepl is not searching my string and returning any rows to match with the actual
        If you could explaining why that would be great

        • Please apply the head function to your data frame to show the first six rows of your data, and copy/paste those first six rows here. Otherwise, it will be difficult for me to help you with this.


  • EL FARCHOUNI Abderrahman
    October 3, 2022 10:01 am

    Hello sir
    this is my code :
    R = 6378137.0
    obs$longitude = obs$longitude*pi/180.0
    obs$latitude = obs$latitude*pi/180.0
    X = (obs$longitude – min(obs$longitude))*cos(mean(obs$latitude))*R
    Y = (obs$latitude – min(obs$latitude))*R
    Z = obs$meters
    $the result
    Error in `$<-.data.frame`(`*tmp*`, longitude, value = numeric(0)) :
    replacement has 0 rows, data has 49

    • Hey,

      It seems like your original data does not contain a variable called longitude. Could you please run the following code and share the output?



  • Hi Joachim,

    I’ve just seen this webpage&your post. I’m new to R bibliometrix. I have a problem with my coding. can you help me ?

    data_b$condition <- factor(data_b$randnumber, levels = c(1,2), labels = c("direkt","crosswise"))
    #Stichprobenzahl gesamt

    N_overall <- length(data$id)
    N_adjusted <-length(data_a$id)

    Error in `$<-.data.frame`(`*tmp*`, condition, value = integer(0)) : replacement has 0 rows, data has 21


  • Hi Joachim,

    thanks for your video. I get a similar warning message but can’t figure out what the problem is.

    Running the following code:

    x %
    group_by(cyl) %>%
    nest() %>%

    y %
    filter(c(FALSE, FALSE, FALSE))

    I get the warning:

    Warning in `[<-.data.frame`(`*tmp*`, is_list, value = list(`2` = "”)) :
    Erstzungselement 1 hat 1 Zeile, um 0 Zeilen zu ersetzen

    Could you help?

  • Sorry my code is not shown correctly

    x %
    group_by(cyl) %>%
    nest() %>%

    y %
    filter(c(FALSE, FALSE, FALSE))

  • Luke Rogers
    June 28, 2023 5:24 pm

    Hi Joachim,

    Thank you for this post, it has at least gotten me headed in the right direction. I do have a question though that is related to this but slightly different. I have a large spatial points data frame, which I am using to do a probability prediction with Support Vector Machine Learning. I have NA values in my original raster from which the spatial points were made. The SVM predictions produce nothing where an input is NA in the raster/spatial points. Now I want to attribute the probability predictions back to the spatial points like this:

    d$pred <- attr(p, "probabilities")[,2]

    getting error:
    Error in `[[<-.data.frame`(`*tmp*`, name, value = c(`48` = 0.0754259426544666, :
    replacement has 2023 rows, data has 5775

    …which is expected as the predictions only exist where there were no NA's in the spatial points. How can I accomplish this? Do I need an NA assignment first followed by a where clause to filter out only the points where there are no NA's?



Leave a Reply

Your email address will not be published. Required fields are marked *

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