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.
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.
39 Comments. Leave new
It doesn’t work, same error
Hey Toni,
Could you share your code? I can have a look.
Regards
Joachim
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? 🙂
Regards
Joachim
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:
Regards,
Joachim
Thanku soo much it work now
This is great to hear Ayesha, glad it helped!
Regards,
Joachim
Hi Joachim,
I solved the problem, 🙂
Thanx&Regards
Erkan
Hey Erkan,
This is great to hear! Thanks for sharing 🙂
Regards
Joachim
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?
Regards,
Joachim
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
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
Hi , I have some problms, as follows,
Error in `$<-.data.frame`(`*tmp*`, "OrderTPL", value = NA_character_)
Hey Sijie,
Could you share the code you have used?
Regards,
Joachim
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.
Hey Laone,
Could you share the code you have used so far, and illustrate the structure of your data?
Regards,
Joachim
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?
Regards,
Joachim
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
Hey Lisa,
Does the following code work for you?
Regards,
Joachim
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
Hey Jovin,
Could you share the code that has resulted in this error message?
Regards,
Joachim
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.
Regards,
Joachim
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?
Regards,
Joachim
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.
Regards,
Joachim
Hello sir
this is my code :
obs<-read.csv("sample_data.txt")
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?
Regards,
Joachim
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
Regards,
Bona
Hello Bona,
Could you please give more information about your variable randnumber? The solution in this tutorial hasn’t worked for you, I assume, is that correct?
Regards,
Cansu
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() %>%
ungroup()
y %
filter(c(FALSE, FALSE, FALSE))
y
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() %>%
ungroup()
y %
filter(c(FALSE, FALSE, FALSE))
y
Hello Matthias,
For which command do you get this error?
Regards,
Cansu
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?
Thanks!
Hello Luke,
It looks like you should handle the missingness before the prediction. It is better that you decide the best approach based on some data exploration. Here is our intro tutorial about handling missingness: Missing Values – Statistical Analysis & Handling of Incomplete Data. This could be a starting point for you.
Best,
Cansu