更改R中列的类别以匹配其他数据集的类别的函数 [英] Function to change class of columns in R to match the class of an other dataset
问题描述
我试图编写一个函数来更改数据集中变量的类,以匹配另一个数据集的列的类.
I am trying to write a function that changes the class of the variables in a dataset in order to match the class of the column of an other dataset.
例如,如果我有一个数据集1,其中有一个名为A的列作为类因子,而一个数据集2的有一个名为A的列具有类字符,我想在数据集中更改列A的类2为因数.
By example, if I have a dataset 1 with a column named A which as a class factor, and a dataset 2 with a column named A which has a class character, I want to change the class of the column A in dataset 2 to factor.
我有一个代码,但我不明白为什么,它无法更改类别.你有什么主意吗?
I have a code, but I don't understand why, it fails to change the classe. Do you have any idea?
change_class2=function(predict_set,train_set){
col_drop=c()
for(column in colnames(predict_set)){
if(!column %in% colnames(train_set))
{col_drop=c(col_drop,column)}
v=grep(column, colnames(predict_set))
w=grep(column, colnames(train_set))
if((class(predict_set[,v])!=class(train_set[,w]))*(is.factor(train_set[,w]))==1){
predict_set[,v]=factor(predict_set[,v])}
else if((class(predict_set[,v])!=class(train_set[,w]))*(is.character(train_set[,w]))==1){
predict_set[,v]=as.character(predict_set[,v])}
else if((class(predict_set[,v])!=class(train_set[,w]))*(is.numeric(train_set[,w]))==1){
predict_set[,v]=as.numeric(predict_set[,v])}
else if((class(predict_set[,v])!=class(train_set[,w]))*(is.integer(train_set[,w]))==1){
predict_set[,v]=as.integer(predict_set[,v])}
else{
predict_set[,v]=predict_set[,v]}
}}
推荐答案
您可以使用 class
函数将一个类分配给对象:
You can use the class
function to assign a class to an object:
class(predict_set[, v]) <- class(train_set[, w])
更进一步,您可以将列称为字符串而不是其索引,这样您就可以取出 v
和 w
grep
语句,只需使用列
:
Taking it a step further, you can refer to columns as strings instead of their indices so you can take out your v
and w
grep
statements and just use column
:
class(predict_set[, column]) <- class(train_set[, column])
所以在一起:
change_class2 <- function(predict_set, train_set) {
for (column in colnames(predict_set) {
class(predict_set[, column]) <- class(train_set[, column])
}
}
这篇关于更改R中列的类别以匹配其他数据集的类别的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!