簡體   English   中英

dplyr字符串作為列引用

[英]dplyr string as column reference

無論如何將字符串作為列引用傳遞給dplyr過程?

下面是一個示例 - 使用分組數據集和一個簡單函數,我嘗試將字符串作為引用傳遞給列。 謝謝!

machines <- data.frame(Date=c("1/31/2014", "1/31/2014", "2/28/2014", "2/28/2014", "3/31/2014", "3/31/2014"), 
            Model.Num=c("123", "456", "123", "456", "123", "456"), 
            Cost=c(200, 300, 250, 350, 300, 400))

my.fun <- function(data, colname){
    mutate(data, position=cumsum(as.name(colname)))
}

machines <- machines %>% group_by(Date, Model.Num)     
machines <- my.fun(machines, "Cost")

這是一個使用lazyeval包中的interp interp()的選項, interp()dplyr安裝一起提供。 在您的函數內部,您需要使用dplyr函數的標准評估版本。 在這種情況下,它將是mutate_()

請注意,由於您在machines設置分組的方式,新列position將與此處的“ Cost列相同。 my_fun()的第二次調用顯示它正在處理一組不同的分組變量。

library(dplyr)
library(lazyeval)

my_fun <- function(data, col) {
    mutate_(data, position = interp(~ cumsum(x), x = as.name(col)))
}

my_fun(machines, "Cost")
#        Date Model.Num Cost position
# 1 1/31/2014       123  200      200
# 2 1/31/2014       456  300      300
# 3 2/28/2014       123  250      250
# 4 2/28/2014       456  350      350
# 5 3/31/2014       123  300      300
# 6 3/31/2014       456  400      400

## second example - different grouping
my_fun(group_by(machines, Model.Num), "Cost")
#        Date Model.Num Cost position
# 1 1/31/2014       123  200      200
# 2 1/31/2014       456  300      300
# 3 2/28/2014       123  250      450
# 4 2/28/2014       456  350      650
# 5 3/31/2014       123  300      750
# 6 3/31/2014       456  400     1050

我們可以在不使用lazyeval包的標准評估中進行評估。 我們可以使用setNames將一些字符串設置為變量名。

library(tidyverse)

machines <- data.frame(
  Date = c("1/31/2014", "1/31/2014", "2/28/2014", "2/28/2014", "3/31/2014", "3/31/2014"), 
  Model.Num = c("123", "456", "123", "456", "123", "456"), 
  Cost = c(200, 300, 250, 350, 300, 400)
  )

my_fun <- function(data, col) {
  mutate_(data, .dots = setNames(paste0("cumsum(", col, ")"), "position"))
}

my_fun(machines %>% group_by(Date, Model.Num), "Cost")
# Source: local data frame [6 x 4]
# Groups: Date, Model.Num [6]
# 
# Date Model.Num  Cost position
# <fctr>    <fctr> <dbl>    <dbl>
# 1 1/31/2014       123   200      200
# 2 1/31/2014       456   300      300
# 3 2/28/2014       123   250      250
# 4 2/28/2014       456   350      350
# 5 3/31/2014       123   300      300
# 6 3/31/2014       456   400      400
my_fun(machines %>% group_by(Model.Num), "Cost")
# Source: local data frame [6 x 4]
# Groups: Model.Num [2]
# 
# Date Model.Num  Cost position
# <fctr>    <fctr> <dbl>    <dbl>
# 1 1/31/2014       123   200      200
# 2 1/31/2014       456   300      300
# 3 2/28/2014       123   250      450
# 4 2/28/2014       456   350      650
# 5 3/31/2014       123   300      750
# 6 3/31/2014       456   400     1050

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM