[英]Mysterious error while passing column name to a function in R
由於某些機器學習算法無法處理factor
,因此我正在創建一個在數據框內滑動並將factor
變量散布到新的虛擬變量的函數。 為此,我在清潔函數內使用了spread()
函數。
但是,當我嘗試傳遞列名時,我需要傳播,但會引發錯誤:
Error: Invalid column specification
這是代碼:
library(tidyr)
library(dplyr)
library(C50) # this is one source for the churn data
data(churn)
f <- function(df, name) {
df$dummy <- c(1:nrow(df)) # create dummy variable with unique values
df <- spread(df, key <- as.character(substitute(name)), "dummy", fill = 0 )
}
churnTrain = f(churnTrain, name = "state")
str(churnTrain)
當然,如果我將key = as.character(substitute(name))
替換為key = "state"
則可以正常工作,但整個函數會失去其可重用性。
如何將列名正確傳遞給內部函數?
您需要使用tidyverse
嗎?
如果沒有,您可以嘗試使用較舊的reshape2
軟件包:
library(reshape2)
library(C50) # this is one source for the churn data
data(churn)
f <- function(df1, name) {
df1$dummy <- 1:nrow(df1) # create dummy variable with unique values
df1 <- dcast(df1, as.formula(paste0("dummy~", name)))
}
ct1 <- f(churnTrain, name = "state")
如果您絕對需要在tidyverse
工作, tidyverse
可以嘗試按照http://dplyr.tidyverse.org/articles/programming.html上的教程進行操作。 不幸的是,他們的例子在我的機器上不起作用。
library(tidyr)
library(dplyr)
library(C50) # this is one source for the churn data
data(churn)
f <- function(df, name) {
df$dummy <- c(1:nrow(df)) # create dummy variable with unique values
df <- spread_(df, key = name, "dummy", fill = 0 )
}
churnTrain = f(churnTrain, name = "state")
str(churnTrain)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.