[英]Cannot pass column names to function in r
I try to create a function to generate dummy variables, but I find the column name cannot be recognized when I create a trail function.我尝试创建一个 function 来生成虚拟变量,但是我发现在创建跟踪 function 时无法识别列名。
here is my code:这是我的代码:
library(tidyverse)
library(tidyr)
library(gridExtra)
## set the file path
file = "https://raw.githubusercontent.com/Carloszone/Kaggle-Cases/main/01-Titanic/train.csv"
## load data and name it "dat_train"
dat_train = read.csv(file)
## transform columns' data types
dat_train <- dat_train %>% transform(PassengerId = as.character(PassengerId),
Survived = as.factor(Survived),
Pclass = as.factor(Pclass),
Sex = as.factor(Sex),
SibSp = as.factor(SibSp),
Parch = as.factor(Parch),
Ticket = as.character(Ticket),
Cabin = as.character(Cabin),
Embarked = as.factor(Embarked)
)
## create functions
x <- function(data, name){
dummy <- model.matrix(~name, data)[,-1] %>% head()
return(dummy)
}
y <- function(data){
dummy <- model.matrix(~Pclass, data)[,-1] %>% head()
return(dummy)
}
## test functions
x(dat_train, "Pclass")
y(dat_train)
At first, I create the function "x", but I find it doesn't work:起初,我创建了 function "x",但我发现它不起作用:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Therefore, I create the function "y", and it runs well.因此,我创建了 function "y",它运行良好。
Pclass2 Pclass3
1 0 1
2 0 0
3 0 1
4 0 0
5 0 1
6 0 1
So, I think the question is the column name fail to pass to the function.所以,我认为问题是列名无法传递给 function。 But I don't know how to deal with the problem.但我不知道如何处理这个问题。
You could make your function work by using eg as.formula
:您可以使用例如as.formula
使您的 function 工作:
## create functions
x <- function(data, name){
fmla <- as.formula(paste("~", name))
dummy <- model.matrix(fmla, data)[,-1] %>% head()
return(dummy)
}
## test functions
x(dat_train, "Pclass")
#> Pclass2 Pclass3
#> 1 0 1
#> 2 0 0
#> 3 0 1
#> 4 0 0
#> 5 0 1
#> 6 0 1
Created on 2021-01-02 by the reprex package (v0.3.0)由代表 package (v0.3.0) 于 2021 年 1 月 2 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.