将变量传递给tidyr的聚集以重命名键/值列?

Jef*_*eff 6 r tidyr

我想tidyr::gather()在一个自定义函数内部调用,我传递一对将用于重命名keyvalue列的字符变量.例如

myFunc <- function(mydata, key.col, val.col) {
    new.data <- tidyr::gather(data = mydata, key = key.col, value = val.col)
    return(new.data)    
}
Run Code Online (Sandbox Code Playgroud)

但是,这不能按预期工作.

temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45), day.3 = c(17, 9, 33))

# Call my custom function, renaming the key and value columns 
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col = "temp")

# Columns have *not* been renamed as desired
head(long.data)
  key.col val.col
1   day.1      20
2   day.1      22
3   day.1      23
4   day.2      32
5   day.2      22
6   day.2      45
Run Code Online (Sandbox Code Playgroud)

期望的输出:

head(long.data)
    day temp
1 day.1   20
2 day.1   22
3 day.1   23
4 day.2   32
5 day.2   22
6 day.2   45
Run Code Online (Sandbox Code Playgroud)

我的理解是gather()对大多数参数使用裸变量名称(在本例中,使用"key.col"列名而不是存储的key.col).我尝试了多种方法在gather()调用中传递值,但大多数返回错误.例如,返回中gather()调用的这三个变体(为了便于说明,忽略具有相同行为的参数): myFuncError: Invalid column specificationvalue

gather(data = mydata, key = as.character(key.col) value = val.col)

gather(data = mydata, key = as.name(key.col) value = val.col)

gather(data = mydata, key = as.name(as.character(key.col)) value = val.col)
Run Code Online (Sandbox Code Playgroud)

作为一种解决方法,我只需在调用后重命名列gather():

colnames(long.data)[colnames(long.data) == "key"] <- "day"
Run Code Online (Sandbox Code Playgroud)

但鉴于gather()用于重命名键/值列的声称功能,我如何gather()在自定义函数中的调用中执行此操作?

Bry*_*gin 3

要将其放入函数中,您必须gather_()像这样使用。

myFunc <- function(mydata, key.col, val.col, gather.cols) {
  new.data <- gather_(data = mydata,
                      key_col = key.col,
                      value_col = val.col,
                      gather_cols = colnames(mydata)[gather.cols])
  return(new.data)    
}

temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data


     day.1 day.2 day.3
1    20    32    17
2    22    22     9
3    23    45    33

# Call my custom function, renaming the key and value columns 
# "day" and "temp", respectively

long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =   
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)

  day temp
1 day.1   20
2 day.1   22
3 day.1   23
4 day.2   32
5 day.2   22
6 day.2   45
Run Code Online (Sandbox Code Playgroud)

如前所述,主要区别在于gather_您必须指定要使用参数收集的列gather_cols

  • 很好的解释。我没有意识到威克姆给不起眼的下划线赋予了如此强大的力量。 (2认同)