I am trying to achieve something similar to this and this question but instead of replacing with a single value I would like to replace based on another column.
The data is from particular sites that have 'yes' when measurements were taken on a specific date and I now would like the water level of the river on those particular dates. My data looks something like this (but with many more sites and dates):
date <- c('2000-01-01','2000-01-02','2000-01-03','2000-01-04','2000-01-05','2000-01-06','2000-01-07','2000-01-08','2000-01-09','2000-01-10')
date <- as.Date(date)
Site1 <- c('yes','','yes','','yes','','yes','','','')
Site2 <- c('yes','yes','yes','','','','','yes','','yes')
Site3 <- c('','','','','','','yes','yes','','yes')
waterlevel <- c(24,58,2,38,18,59,20,98,16,88)
df <- data.frame(date,Site1,Site2,Site3,waterlevel)
which gives this dataframe:
date Site1 Site2 Site3 waterlevel
1 2000-01-01 yes yes 24
2 2000-01-02 yes 58
3 2000-01-03 yes yes 2
4 2000-01-04 38
5 2000-01-05 yes 18
6 2000-01-06 59
7 2000-01-07 yes yes 20
8 2000-01-08 yes yes 98
9 2000-01-09 16
10 2000-01-10 yes yes 88
and I would like the output dataframe to look like this:
date Site1 Site2 Site3 waterlevel
1 2000-01-01 24 24 24
2 2000-01-02 58 58
3 2000-01-03 2 2 2
4 2000-01-04 38
5 2000-01-05 18 18
6 2000-01-06 59
7 2000-01-07 20 20 20
8 2000-01-08 98 98 98
9 2000-01-09 16
10 2000-01-10 88 88 88
I have tried using the solution code to the above questions with a match argument with the water level in a different dataframe but the solutions did not work:
sel <- grepl("Site",names(df))
df[sel] <- lapply(df[sel], function(x) replace(x,x %in% 2:4, df2$waterlevel[match(df$date,df2$date)]) )
Any help would be much appreciated.