I have a large dataframe (called dftot) with multiple environmental variable columns, including depth, salinity and treatment locations. The same treatment locations are used multiple times. To simplify:
Depth<-c(1,4,33,7,8,20,12,8)
Treatment<- c("1.1", "1.2", "1.3", "2.1", "2.2", "2.3", "1.1", "1.2")
dftot<- data.frame(Depth, Treatment)
And a (for now) empty column for salinity:
dftot[, "Salinity"] <- NA
Furthermore, for each treatment location I have a dataframe containing Depth and Salinity. Depths go from 1-40 and I will give the salinity random numbers here. For treatment 1.1 it looks something like:
Depth11<- c(1:40)
Salinity11 <- c(data$newrow <- sample(40, size = nrow(data), replace = TRUE)
tr11<- data.frame(Depth11, Salinity11)
What I need is a piece of code that for each treatment in dftot, selects the corresponding treatment dataframe and fills in the salinity value of that treatment dataframe into the empty salinity column in dftot, based on corresponding Depths.
As I have to do this for multiple treatments it would be ideal to have some sort of loop I think. But if this is not possible I could run the code for each treatment as well.
Greatly appreciated if someone can help me out!