consider my labelled df1 below
This is my second dataframe df2
I want to change item column in df2 such that if its rows contains any names of df1, that string is replaced by the column label like below
any approach to achieve this is highly appreciated.
library(Hmisc)
library(dplyr)
df1 <- data.frame(low = rep(1,3),
med = rep(2,3),
high = rep(3,3),
other = rep(0,3))
label(df1$low) <- "is it low"
label(df1$med) <- "is it med"
label(df1$high) <- "is it high"
label(df1$other) <- "is it broken"
df2 <- data.frame(item = c("lowYes", "medNo", "high"),
value = c(12, 10, 14))
df3 <- data.frame(item = c("is it low:No", "is it med:Yes", "is it high"),
value = c(12, 10, 14))
library(stringr)
df2$item <- str_replace(df2$item, grep(df2$item, names(df1)), label(df1)) # not for all rows


