I have a simple data frame:
> var_body_part <- c("eye and nose", "eye", "eye and ear", "eye and mouth", "foot", "foot", "ear", "ear", "foot", "mouth")
> var2 <- c("bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla")
> temp_df <- data.frame(var_body_part, var2)
So my data is:
> temp_df
var_body_part var2
1 eye and nose bla
2 eye bla
3 eye and ear bla
4 eye and mouth bla
5 foot bla
6 foot bla
7 ear bla
8 ear bla
9 foot bla
10 mouth bla
Each time I find "eye" I want to replace the row with HEAD i.e. (see first 4 lines)
var_body_part var2
1 HEAD bla
2 HEAD bla
3 HEAD bla
4 HEAD bla
5 foot bla
6 foot bla
7 ear bla
8 ear bla
9 foot bla
10 mouth bla
It should be easy... I select the rows that are affected by the transformation with
temp_df$var_body_part[grep("eye", temp_df$var_body_part) ]
however I cannot find the correct statement to replace them with the correct value "HEAD".
So far with my attempts I get a lot of
invalid factor level, NA generated
Anybody can help?