R中利用 apply() 将数据框中 factor 类型变量改为数值型

举个例子

library(mlbench)
data(BreastCancer)
str(BreastCancer)

此时输出为:
《R中利用 apply() 将数据框中 factor 类型变量改为数值型》

BreastCancer1 <- apply(BreastCancer[,2:10],2,as.numeric)     # 将2-10列的数据变成数值型
BreastCancer <- data.frame(BreastCancer1, BreastCancer[,11])      
names(BreastCancer)[names(BreastCancer) == 'BreastCancer...11.'] <- 'class'
str(BreastCancer)

此时输出结果为:
《R中利用 apply() 将数据框中 factor 类型变量改为数值型》

    原文作者:yqyang1
    原文地址: https://blog.csdn.net/yqyang1/article/details/106154984
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞