I'm trying to add a new column to my df that is simply my function hardfunct applied to 'values' where the row is 'hardness'. I would then like that value to fill all the rows in that column for the matching 'site' and 'dates'. How do I fill the rest of the rows? I've tried using summarise, rowwise and mutate. Sample data is below.
site=c(rep("River A",4),rep("River B",4))
dates=as.Date(c("01/01/2001","01/01/2001","01/01/2001","01/01/2001","05/08/2001","05/08/2001","05/08/2001","05/08/2001"), format = "%m/%d/%Y")
param=c("lead","hardness","mercury","cadmium","lead","hardness","mercury","cadmium")
value=c("0.2","45","0.9","1.2","0.5","1800","0.6","0.8")
df=data.frame(site,param,dates,value)
hardfunct=function(x){
if (x>=400) {
print(400)
} else if (x<=25) {
print(25)
} else {
return(x)}
}
#######Trying to use group_by and mutate
df %>% group_by(site,dates) %>%
mutate(New_Hardness=sapply(df[df$param=="hardness","value"],hardfunct))
This is what data frame with the new column should look like
site param dates value New_Hardness
River A lead 1/1/2001 0.2 45
River A hardness 1/1/2001 45 45
River A mercury 1/1/2001 0.9 45
River A cadmium 1/1/2001 1.2 45
River B lead 5/8/2001 0.5 400
River B hardness 5/8/2001 1800 400
River B mercury 5/8/2001 0.6 400
River B cadmium 5/8/2001 0.8 400