2013-10-06 43 views
0

我是R和机器学习算法的新手,并试图使用kaggle scikit example来学习。从一个数据帧中添加列作为另一个数据帧的最后一列

我有以下两个数据帧:

> str(d.train) 
'data.frame': 1000 obs. of 40 variables: 
$ V1 : num 0.299 -1.174 1.192 1.573 -0.613 ... 

> str(d.trainLabels) 
'data.frame': 1000 obs. of 1 variable: 
$ V1: int 1 0 0 1 0 1 0 1 1 0 ... 

从我的理解,大多数布线工具是为了用相同的数据帧中的类信息使用。出于这个原因,我试图将trainLabels作为最后一列添加到列车数据框中。

我曾尝试下面的代码:

# http://www.gm.fh-koeln.de/~konen/WPF-DM-Cup/DM-Template/ClassifyTemplate/utils_DMC.r 
###################################################################################### 
# bind the column with name response.predict and contents vec as last column 
# to data frame d 
###################################################################################### 
bind_response <- function(d,response.predict,vec) 
{ 
    # drop column response.predict if there, do nothing if not there 
    d <- d[,setdiff(names(d),response.predict)] 
    # bind column response.predict as last column to data frame d 
    d <- cbind(d, prediction=vec) 
    names(d)[names(d)=="prediction"] <- response.predict 

    return(d) 
} 

d.totalTrain <- bind_response(d.train, d.trainLabels, "1") 

,但我不知道结果是什么,我想:

> str(d.totalTrain) 
'data.frame': 1000 obs. of 41 variables: 
... 
$ V40                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                : num 0.101 -1.818 2.987 1.883 0.408 ... 
$ c(1, 0, 0, 1, 0, 1, 0, 1, ... 
+0

*你想要什么结果? –

+0

我想要的是:$ V41:int 1 0 0 1 0 1 0 1 1 0 ... – tucson

回答

2

是否重新命名它首先你想要做什么?

colnames(d.trainLabels) <- "V41" 
cbind(d.train, d.trainLabels) 
+0

如何访问cbind的结果?好的:m < - cbind()。这样可行。谢谢。 – tucson

+0

准确。注:如果你不确定在两个数据框架之间的顺序是相同的,你可以使用'merge'代替。不过,在这种情况下,您可以使用'cbind'安全。 –

相关问题