Home > OS >  variable is not recognised inside function
variable is not recognised inside function

Time:03-18

I have a dataset in the following format:

Date Time efx jpd Nation
01/01/22 10:00 9.2 7.9 UK
01/01/22 10:10 8.9 8.5 UK
01/01/22 10:20 9.5 8.5 USA
01/01/22 10:30 9.1 8.7 IRE
... ... ... ... ...

I'm trying to get it in this format where efx are the values:

Date IRE USA UK ...
01/01/22 8.7 9.2 7.9
01/01/22 8.4 8.9 8.5
01/01/22 8.5 9.5 8.5
01/01/22 8.4 9.1 8.7
... ... ... ... ...

If I execute this code then it achieves what I'm after:

newdata <- data %>% 
  select(date, nation, efx) %>% 
  mutate(id=row_number()) %>% 
  pivot_wider(names_from = nation, values_from = efx) %>% 
  select(-id)

But if I create a function so that multiple datasets are created where the variable in the values_from changes:

Data.func<- function(var){
  data %>% 
    select(date, nation, var) %>% 
    mutate(id=row_number()) %>% 
    pivot_wider(names_from = nation, values_from = var) %>% 
    select(-id)
}
Data.func(efx)
Data.func(jpd)

the variable in the function isn't recognised:

Error: object 'efx' not found
Run `rlang::last_error()` to see where the error occurred. 

Can anyone help?

CodePudding user response:

You can make use of curly curly ({{..}}) to pass variables in a function.

library(dplyr)
library(tidyr)

Data.func<- function(data, var){
  data %>% 
    select(Date, Nation, {{var}}) %>% 
    mutate(id=row_number()) %>% 
    pivot_wider(names_from = Nation, values_from = {{var}}) %>% 
    select(-id)
}

Data.func(data, efx)

#  Date        UK   USA   IRE
#  <chr>    <dbl> <dbl> <dbl>
#1 01/01/22   9.2  NA    NA  
#2 01/01/22   8.9  NA    NA  
#3 01/01/22  NA     9.5  NA  
#4 01/01/22  NA    NA     9.1

Data.func(data, jpd)

#  Date        UK   USA   IRE
#  <chr>    <dbl> <dbl> <dbl>
#1 01/01/22   7.9  NA    NA  
#2 01/01/22   8.5  NA    NA  
#3 01/01/22  NA     8.5  NA  
#4 01/01/22  NA    NA     8.7

data

data <- structure(list(Date = c("01/01/22", "01/01/22", "01/01/22", "01/01/22"
), Time = c("10:00", "10:10", "10:20", "10:30"), efx = c(9.2, 
8.9, 9.5, 9.1), jpd = c(7.9, 8.5, 8.5, 8.7), Nation = c("UK", 
"UK", "USA", "IRE")), row.names = c(NA, -4L), class = "data.frame")
  • Related