I want to create a function which renames specific values in a column to something else, which is specified by the function, something like this (although in reality there would be much more to rename):
func <- function(x) x %>%
mutate(col_name = ifelse(col_name =="something","something else",
ifelse(col_name == "something2","something_else2")))
Note that it isn't the column names that I want to change, it is the values themselves in the column. However, I would like this to work regardless of which column the values are in (e.g. the function works all over the data frame). Also, this only works if the values named in the function is present, and I would like it to ignore the ones that aren't present in the columns. here is a small reproducible example: (column values are arbitrary)
col1 <- c("a","b","c","d","e")
col2 <- c("b","f","d","c","g")
df <- data.frame(col1, col2)
col3 <- c("a","h","i","b","c")
col4 <- c("c","d","j","a","g")
df2 <- data.frame(col3, col4)
Which looks like this:
df1:
col1 col2
1 a b
2 b f
3 c d
4 d c
5 e g
df2:
col3 col4
1 a c
2 h d
3 i j
4 b a
5 c g
Say that i want to rename like this:
df1:
col1 col2
1 can chi
2 chi pig
3 equ she
4 she equ
5 fox bov
df2:
col3 col4
1 can equ
2 avi she
3 tyr asp
4 chi can
5 equ bov
So what I was hoping to get was a function that changes the names of multiple values in data frame columns regardless of its position in the data frame, and that it ignores the values not found in the data frame by the function.
df2has a typo, the first row ofcol4should beequ?