Home > Back-end >  take value from lookup column conditional on other lookup column that is matching column names from
take value from lookup column conditional on other lookup column that is matching column names from

Time:03-20

I have this dataframe:

df <- tribble(
  ~original1,   ~original2,
1,  3,
2,  4)

  original1 original2
      <dbl>     <dbl>
1         1         3
2         2         4

and I have this lookup table:

lookup <- structure(list(original_names = c("original1", "original2", "original3"
), handy_names = c("handy_original1", "handy_original2", "handy_original3"
), label_names = c("label_original1", "label_original2", "label_original3"
)), class = "data.frame", row.names = c(NA, -3L))

  original_names     handy_names     label_names
1      original1 handy_original1 label_original1
2      original2 handy_original2 label_original2
3      original3 handy_original3 label_original3

I want to assign label_names as label attribute to dataframe df

I have tried so far:

library(sjlabelled)
library(dplyr)
df %>% 
  set_label(label = lookup$label_names[lookup$original_names %in% names(df)])

# gives
> get_label(df)
original1 original2 
       ""        "" 

My desired output

> get_label(df)
original1         original2 
"label_original1" "label_original2" 

CodePudding user response:

Try the assignment

set_label(df) <- lookup$label_names[lookup$original_names %in% names(df)]

-testing

> get_label(df)
        original1         original2 
"label_original1" "label_original2" 

Note that when we are using set_label, it is not assigning in place, i.e. we may need to assign back to 'df'

library(dplyr)
df <- df %>% 
  set_label(label = lookup$label_names[lookup$original_names %in% names(df)])

Now check

> get_label(df)
        original1         original2 
"label_original1" "label_original2" 
  • Related