2

似乎在 tidyr 中发现了一个错误。

我有一段这样的代码

 rm(hello)
 a <- function() {  
  dt <- data.table(`*stat*` = c("mean","max"), time = c(11, 12), val1 = c(1,2), val2 =c(3,4))
  hello <- "^(time)$"
  dt2 <- dt %>% gather(dimension, value, -matches("\\*stat\\*"), -matches(hello))  
}

a()

    dt <- data.table(`*stat*` = c("mean","max"), time = c(11, 12), val1 = c(1,2), val2 =c(3,4))
hello <- "^(time)$"
dt2 <- dt %>% gather(dimension, value, -matches("\\*stat\\*"), -matches(hello))  

作品。

唯一的区别是一组代码在一个函数中,而另一组不在!

4

1 回答 1

0

我只是在此处添加一个答案,以显示函数和独立代码在dt2添加到a()函数的最后一行后具有相同的结果(如注释中的 @A5C1D2H2I1M1N2O1R2T1 所建议)。

library(tidyverse)

a <- function() {  
  dt <- data.table::data.table(`*stat*` = c("mean","max"), time = c(11, 12), val1 = c(1,2), val2 =c(3,4))
  hello <- "^(time)$"
  dt2 <- dt %>% gather(dimension, value, -matches("\\*stat\\*"), -matches(hello)) 
  dt2
}

a()
#>   *stat* time dimension value
#> 1   mean   11      val1     1
#> 2    max   12      val1     2
#> 3   mean   11      val2     3
#> 4    max   12      val2     4

dt <- data.table::data.table(`*stat*` = c("mean","max"), time = c(11, 12), val1 = c(1,2), val2 =c(3,4))
hello <- "^(time)$"
dt2 <- dt %>% gather(dimension, value, -matches("\\*stat\\*"), -matches(hello))  
dt2
#>   *stat* time dimension value
#> 1   mean   11      val1     1
#> 2    max   12      val1     2
#> 3   mean   11      val2     3
#> 4    max   12      val2     4

identical(a(), dt2)
#> [1] TRUE
于 2017-09-27T09:39:46.430 回答