【问题标题】:One more tidyr, gather and use of variables一个更整洁,收集和使用变量
【发布时间】:2018-05-18 09:25:27
【问题描述】:

这是我的代码:

library(tidyr)

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

我想将gather 函数与变量/函数结果一起使用。借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

我错过了什么?

【问题讨论】:

    标签: r variables tidyr


    【解决方案1】:

    使用最新版本的 tidyverse 函数,不鼓励您使用函数的下划线版本进行标准评估,而是使用 rlang 函数语法。在这种情况下,您可以使用

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

    【讨论】:

    • 快速提问。对来自dplyrrename 执行相同的方法
    • 应该的。如果您有问题,请随时发布新问题,但请先浏览本网站,因为这类问题有些常见。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2011-08-28
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多