In my data, I construct synthetic estimates for all observations within a data.frame. However, for some observations, there are observed values that I would like to use instead of the synthetic estimates. Over my real data, the observed information varies depending upon years, crop type, and county. So I am trying to construct something general that could be used to conditionally replace this information depending upon what is actually observed. I've made a trivial example to show you what I mean.
#Ideal Example: It works because everything is in the proper order
set.seed(1234)
df <- data.frame(Name = LETTERS[1:8], Estimated = 5*rnorm(8))
df
alt.df <- data.frame(Name = c('A', 'F'), Observed = 3*runif(2))
alt.df
df$Combined[df$Name %in% alt.df$Name] <- alt.df$Observed
df$Combined[is.na(df$Combined)] <- df$Estimated[is.na(df$Combined)]
df
#Example doesn't work because the order of alt.df$Name is set as (F, A)
set.seed(1234)
df <- data.frame(Name = LETTERS[1:8], Estimated = 5*rnorm(8))
df
alt.df <- data.frame(Name = c('F', 'A'), Observed = 3*runif(2))
alt.df
#Error is that values fo "F" = 0.85.. is input as value for "A"
df$Combined[df$Name %in% alt.df$Name] <- alt.df$Observed
df$Combined[is.na(df$Combined)] <- df$Estimated[is.na(df$Combined)]
df
I've struggled through this for the last several days and have looked hard at other Stack Overflow posts including:
Replace a value in a data frame based on a conditional (`if`) statement in R
Changing values in list if that value meets criteria in R
and numerous others.
They have a load of information and I have worked through their examples, but I still cannot figure out how to generalize their solutions to my case where I am not trying to replace a single value, but pulling information from another data set (which might vary) and construct a new variable that merges both the synthetic and observed information into a single variable matched by the identifiers (in the trivial example, the letters). In the trivial example, I've included factors, but I do not have to have factors and actually currently import my data with the option stringsAsFactors = FALSE
. So if it is easier without factors, let me know.
I'm sure it's something simple that I'm missing...