再来一个tidyr,gather和变量的使用
One more tidyr, gather and use of variables
这是我的代码:
library(tidyr)
messy <- data.frame(
name = c("Wilbur", "Petunia", "Gregory"),
a = c(67, 80, 64),
b = c(56, 90, 50)
)
我想使用 gather
函数和 variable/function 结果。借用 from 我试过:
not_messy <-messy %>%
gather_('drug', 'heartrate', paste0('a',''):paste0('b',''))
但它产生了错误:
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
我错过了什么?
对于最新版本的 tidyverse 函数,不鼓励您使用函数的下划线版本进行标准评估,而是使用 rlang 函数语法。在这种情况下,您可以使用
gather(messy, "drug", "heartrate", (!!as.name("a")):(!!as.name("b")))
这是我的代码:
library(tidyr)
messy <- data.frame(
name = c("Wilbur", "Petunia", "Gregory"),
a = c(67, 80, 64),
b = c(56, 90, 50)
)
我想使用 gather
函数和 variable/function 结果。借用 from 我试过:
not_messy <-messy %>%
gather_('drug', 'heartrate', paste0('a',''):paste0('b',''))
但它产生了错误:
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
我错过了什么?
对于最新版本的 tidyverse 函数,不鼓励您使用函数的下划线版本进行标准评估,而是使用 rlang 函数语法。在这种情况下,您可以使用
gather(messy, "drug", "heartrate", (!!as.name("a")):(!!as.name("b")))