Home > Software engineering >  how to replace data frame rows containing column names with column labels in R
how to replace data frame rows containing column names with column labels in R

Time:07-18

consider my labelled df1 below

enter image description here

This is my second dataframe df2

enter image description here

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

enter image description here

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

CodePudding user response:

Extract the label from the 'df1' and create a named vector (unlist), then use the named vector in str_replace_all for modifying the 'item' column by matching the key value with the substring in 'item' column

library(dplyr)
library(stringr)
library(Hmisc)
keyval <- df1 %>% 
  summarise(across(everything(), ~ str_c(label(.x), ":"))) %>% 
  unlist
df3 <- df2 %>%
   mutate(item = trimws(str_replace_all(item, keyval), whitespace = ":"))

-output

df3
           item value
1 is it low:Yes    12
2  is it med:No    10
3    is it high    14
  • Related