【发布时间】:2017-12-15 06:01:06
【问题描述】:
我是 R 的初学者,想知道是否有任何方法可以将多个向量/变量转换为所需的“类”(例如,数据集中的 3 个变量是因子,我想将这 3 个变量转换为数值变量一口气)。
下面是包含列"Product" 为chr 和其余列为factors 的数据集,但是我想保留"Product" 和"Month" 作为字符和"Sales" 和"Profit" 作为数字。
str(Conditional_function_IVY)
'data.frame': 100 obs. of 4 variables:
$ Product: chr "Bellen" "Bellen" "Sunshine" "Sunset" ...
$ Month : Factor w/ 12 levels "April","August",..: 5 5 5 5 5 5 5 5 4 4 ...
$ Sales : Factor w/ 88 levels " ? 501.00 "," ? 504.00 ",..: 8 13 64 16 55 78 81 29 2 52 ...
$ Profit : Factor w/ 65 levels " ? 100.00 "," ? 101.00 ",..: 44 34 5 15 39 16 37 38 65 56 ...
我已经按照以下方式完成了它,但是它消耗了很多时间,因此我想知道是否有任何方法可以让我一次性完成。
Conditional_function_IVY$Month=as.character(Conditional_function_IVY$Month)
> Conditional_function_IVY$Sales=as.numeric(Conditional_function_IVY$Sales)
> Conditional_function_IVY$Profit=as.numeric(Conditional_function_IVY$Profit)
> str(Conditional_function_IVY)
'data.frame': 100 obs. of 4 variables:
$ Product: chr "Bellen" "Bellen" "Sunshine" "Sunset" ...
$ Month : chr "January" "January" "January" "January" ...
$ Sales : num 8 13 64 16 55 78 81 29 2 52 ...
$ Profit : num 44 34 5 15 39 16 37 38 65 56 ...
【问题讨论】:
标签: r