Thanks for your reply and I am sorry that I was too brief:
df1:
eventID <- c(1, 2, 3, 4, 5, 6, 7, 8)
police_force <- c(1, 2, 1, 6, 4, 1, 6, 3)
year <- c(2014, 2014, 2009, 2020, 2015, 2016, 2014, 2015)
day_of_week <- c(7, 2, 2, 1, 5, 4, 5, 3)
df1 <- data.frame(eventID, police_force, year,day_of_week)
code_label
code <- c(1,2,3,4,5,6,1,2,3,4,5,6,7)
label <- c("Force_A", "Force_B","Force_C","Force_D","Force_E","Force_F", "Monday", "Tuesday","Wednesday","Thursday","Friday", "Saturday", "Sunday")
field_name <- c("police_force", "police_force", "police_force", "police_force", "police_force", "police_force", "dayofweek", "dayofweek", "dayofweek", "dayofweek", "dayofweek", "dayofweek", "dayofweek")
code_label <- data.frame(code,label,field_name)
code_label_temp - is code label filtered, in my example by field_name "police_force" (mentioned so that the reason for the function is clearer I hope):
code <- c(1,2,3,4,5,6)
label <- c("Force_A", "Force_B","Force_C","Force_D","Force_E","Force_F")
field_name <- c("police_force", "police_force", "police_force", "police_force", "police_force", "police_force")
code_label_temp <- data.frame(code,label,field_name)
My eventual aim is to loop through the columns in df1 and replace all the codes with labels, being new to functions I just wanted to get the join working before looking at loops. So:
df2 <- df1 %>% left_join(code_label_temp, by = c("police_force"="code"))
gives the result I want.
PROBLEM:
I want to pass the column name in df1 as an argument "statcat" to the function as follows:
f_replace_codes(statcat = "police_force")
f_replace_codes <- function(statcat)
{
df1 <- df2 %>% left_join(code_label_temp, by = c(statcat='code'))
}
but that gives the error:
Error: Join columns must be present in data.
x Problem with `statcat`