error with merge()

So I'm trying to merge the following two datasets:

> dput(head(org_avg_long))
structure(list(Sample = c("Initial Biomass", "Initial Biomass", 
"Initial Biomass", "Initial Biomass", "Initial Biomass", "Initial Biomass"
), Acid = c("Succinic acid", "Lactic acid", "Formic acid", "Acetic acid", 
"Propionic acid", "Isobutyric acid"), avg = c("12.3", "0.2", 
"0.0", "2.2", "0.1", "0.0")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
> dput(head(org_sd_long))
structure(list(Sample = c("Initial Biomass", "Initial Biomass", 
"Initial Biomass", "Initial Biomass", "Initial Biomass", "Initial Biomass"
), Acid = c("Succinic acid", "Lactic acid", "Formic acid", "Acetic acid", 
"Propionic acid", "Isobutyric acid"), sd = c("0.0", "0.0", "0.0", 
"0.5", "0.0", "0.0")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

But when I use the merge function, it's telling me that my columns don't exist:

> org_acid_2<-merge(org_avg_long,org_sd_long,by=c(Sample,Acid))
Error in fix.by(by.x, x) : object 'Sample' not found
> 

I'm not sure why it isn't finding my columns that exist. Any input would be greatly appreciated.

You need to put the column names in the by argument in quotes.

org_acid_2<-merge(org_avg_long,org_sd_long,by=c("Sample","Acid"))
1 Like

That's because, like all base R functions, merge() expects the column names to be text, given in quotes:

merge(org_avg_long, org_sd_long, by = c("Sample","Acid"))

The advantage is that you could store the column name in a variable easily:

sample_col <- "Sample"
merge(org_avg_long, org_sd_long, by = c(sample_col, "Acid"))

You're probably used to {tidyverse} functions, such as left_join(), that tend to accept arguments without quotes.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.