Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

take value from lookup column conditional on other lookup column that is matching column names from other dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" 

>Solution :

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" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading