Home > Enterprise >  How to use labelled::look_for with pipes in R
How to use labelled::look_for with pipes in R

Time:08-25

I want to choose a variable from df and get the same result of e.g. look_for(df, 'country'). Now I get the result for all variables in df.

The names of the variables are on a list called variabs. Is it possible to purrr::map or pmap to get the same results for each variable in the list?

library(tidyverse)
library(labelled)

df <- tibble(country = c(1,2,1),
             sex = c(1,1,2))

df$country <- labelled(
  c(1, 2, 1),
  c(PT = 1, UK = 2),
  label = "Country of birth"
)

df$sex <- labelled(
  c(1, 1, 2),
  c(Male = 1, Female = 2),
  label = "Assigned sex at birth"
)

variabs <- list('country', 'sex')

look_for(df, 'country')
#>  pos variable label            col_type values
#>  1   country  Country of birth dbl lbl  [1] PT
#>                                         [2] UK

showAt <- function(db, v){
  at <- db %>% 
    pull(.data[[v]]) 
  return(look_for(db, at))
}

showAt(df, 'country')
#>  pos variable label                 col_type values    
#>  1   country  Country of birth      dbl lbl  [1] PT    
#>                                              [2] UK    
#>  2   sex      Assigned sex at birth dbl lbl  [1] Male  
#>                                              [2] Female
showAt(df, variabs[[1]])
#>  pos variable label                 col_type values    
#>  1   country  Country of birth      dbl lbl  [1] PT    
#>                                              [2] UK    
#>  2   sex      Assigned sex at birth dbl lbl  [1] Male  
#>                                              [2] Female
Created on 2022-08-24 by the reprex package (v2.0.1)

CodePudding user response:

You could pass each element of variabs into look_for() like this:

purrr::map(variabs, ~ look_for(df, .x))

# [[1]]
#  pos variable label            col_type values
#  1   country  Country of birth dbl lbl  [1] PT
#                                         [2] UK
# 
# [[2]]
#  pos variable label                 col_type values    
#  2   sex      Assigned sex at birth dbl lbl  [1] Male  
#                                              [2] Female

CodePudding user response:

It might be of your interest, that you could also just pass a list or vector directly to look_for. It won't show all variables in the data frame, only the ones specified. See below:

library(labelled)

look_for(df_new, c('country', 'sex'))
look_for(df_new, list('country', 'sex'))

Output:

 pos variable label                 col_type values    
 1   country  Country of birth      dbl lbl  [1] PT    
                                             [2] UK    
 2   sex      Assigned sex at birth dbl lbl  [1] Male  
                                             [2] Female

More data:

df_new <- tibble(country = c(1,2,1),
                 sex = c(1,1,2),
                 third = c(2,1,1))
 
df_new$country <- labelled(
  c(1, 2, 1),
  c(PT = 1, UK = 2),
  label = "Country of birth"
)

df_new$sex <- labelled(
  c(1, 1, 2),
  c(Male = 1, Female = 2),
  label = "Assigned sex at birth"
)

df_new$third <- labelled(
  c(1, 1, 2),
  c(One = 1, Two = 2),
  label = "XX"
)

CodePudding user response:

I found the answer for the first part of my question:

showAt <- function(db, v){
  at <- db %>% 
   select(.data[[v]]) 
  return(look_for(at))
}
  •  Tags:  
  • r
  • Related