user1700890
user1700890

Reputation: 7730

One more tidyr, gather and use of variables

Here is my code:

library(tidyr)

messy <- data.frame(
  name = c("Wilbur", "Petunia", "Gregory"),
  a = c(67, 80, 64),
  b = c(56, 90, 50)
)

And I would like to use gather function with variable/function result. Borrowing from I tried:

not_messy <-messy %>%
  gather_('drug', 'heartrate', paste0('a',''):paste0('b',''))

But it generated error:

Error in paste0("a", ""):paste0("b", "") : NA/NaN argument
In addition: Warning messages:
1: In lapply(.x, .f, ...) : NAs introduced by coercion
2: In lapply(.x, .f, ...) : NAs introduced by coercion

What am I missing?

Upvotes: 1

Views: 66

Answers (1)

MrFlick
MrFlick

Reputation: 206167

With the latest version of the tidyverse functions, you are discouraged from using the underscore versions of the function for standard evaluation and instead use the rlang function syntax. In this case you can use

gather(messy, "drug", "heartrate", (!!as.name("a")):(!!as.name("b")))

Upvotes: 2

Related Questions