簡體   English   中英

將列名傳遞給R中的函數時發生神秘錯誤

[英]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.

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