Use .data[[x]] when x is a column of the data frame

Hi folks,

I would like to refer to a variable when the name of it comes from the value of another variable. Weird stuff, I assume, but it appeared in a real case. Any ideas? Thanks!

da <- tibble::tibble(x = "name", name = 10)

da |> 
  dplyr::summarise(result = name)
#> # A tibble: 1 × 1
#>   result
#>       <dbl>
#> 1        10

da |> 
  dplyr::summarise(result = .data[[x]])
#> Error in splice(dot_call(capture_dots, frame_env = frame_env, named = named, : objeto 'x' não encontrado

Created on 2022-09-24 with reprex v2.0.2

da |> 
  dplyr::summarise(result = cur_data()[[x]])
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.