我有一个数据集,我计划使用不平衡包的ubRacing。但是这个ubRacing只接受数字列。无论如何我可以通过R?
将所有chr列转换为数字谢谢
' data.frame':31000 obs。 22个变量:
$ ID : int 1 2 3 4 5 6 7 8 9 10 ...
$ age : int 56 57 37 40 56 45 59 41 24 25 ...
$ job : chr "housemaid" "services" "services" "admin." ...
$ marital : chr "married" "married" "married" "married" ...
$ education : chr "basic.4y" "high.school" "high.school" "basic.6y" ...
$ default : chr "no" "unknown" "no" "no" ...
$ housing : chr "no" "no" "yes" "no" ...
$ loan : chr "no" "no" "no" "no" ...
$ contact : chr "telephone" "telephone" "telephone" "telephone" ...
$ month : chr "may" "may" "may" "may" ...
$ day_of_week : chr "mon" "mon" "mon" "mon" ...
答案 0 :(得分:1)
目前尚不清楚如何将character
列转换为numeric
。一种可能的选择是将character
类转换为factor
,然后将其强制转换为numeric
。我们使用lapply
循环遍历数据集的列。
df1[] <- lapply(df1, function(x) if(is.character(x)) as.numeric(factor(x))
else (x))