我正在尝试编写一个函数,该函数将数据集和分类变量名称作为输入,并返回具有该分类变量的虚拟变量的数据集。
这是我写的功能:
dummy_func <- function(df_name, df_column, df_out){
full_col_name=paste("df_name","df_column", sep="$")
for (level in (unique(full_col_name))){
df_name[paste("dummy", level, sep="_")] = ifelse(full_col_name == level, 1, 0)
}
}
但是当我在调用函数时传递以下参数时;我得到一个NULL值。
b=dummy_func(grp_new_prof2, "Job_profile")
数据框如下所示:
> grp_new_prof2
job y Freq Job_profile
1 admin. no 0.88 Salaried
2 blue-collar no 0.93 Big-Shots
3 entrepreneur no 0.92 Big-Shots
4 housemaid no 0.91 Home-Service
5 management no 0.86 Salaried
6 retired no 0.77 Non-Salaried
7 self-employed no 0.88 Salaried
8 services no 0.91 Home-Service
9 student no 0.71 Non-Salaried
10 technician no 0.89 Salaried
11 unemployed no 0.84 Non-Salaried
12 unknown no 0.88 Unknown
>
有人可以帮忙吗?