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

TarJae

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" 
akrun

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" 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas Dataframe - Group by column value and lookup values from other columns

Set column values based on lookup from other column

Lookup value from DF column in df col names, take value for corresponding row

Create a new column in a dataframe by conditional matching in dataframe to a "lookup" dataframe

Adding new column to spark dataframe by getting data lookup from other dataframe

Replace Column in Data Frame from Lookup of other Data Frame

Flag consecutive occurrences of value in column based on conditional from other column

How do i use a lookup in excel to return a value from a third column, if criteria are met in 2 other columns

Create a pandas column based on a lookup value from another dataframe

Replacing column value with conditional other column column Dataframe

spark Dataframe column transformations using lookup for values into other Dataframe

how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column?

Sheets - Max value from lookup column

Lookup value from another column that matches with variable

Groupby by a column and select specifc value from other column in pandas dataframe

Adding new Column(s) to a dataframe based on value from other column

How to populate a dataframe column based on a lookup of other columns?

Lookup Matching Value in Other Dataframe and Return ID

Take a column from a Dataframe and normalize all of the other columns against it?

Dataframe new column value based on both other column criteria and calculation from other column

copy column from other dataframe based on matching values

Replacing column names in a pandas dataframe based on a lookup

Lookup a Dataframe column that has rows of lists with list and return matching column value of another column

Lookup value from one dataframe and return in a new column the closest value from another column in another dataframe based on a common value

Adding constant to dataframe column conditional on other column

R add rows to dataframe from other dataframe based on column value

Creating new dataframe with column name and value from other dataframe

Alter the column of dataframe by matching other column with a dictionary

Getting a value from DataFrame based on other column value (PySpark)