Adding column from one data frame as last column of another data frame [duplicate]

StackOverflow https://stackoverflow.com/questions/19209176

  •  30-06-2022
  •  | 
  •  

Question

I am new to R and machine learning algorithms, and trying to learn using the kaggle scikit example.

I have the following two data frames:

> 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 ...

From my understanding, most R tools are meant to be used with the class information inside the same data frame. For this reason I am trying to add the trainLabels as the last column to the train data frame.

I have tried the following code:

# 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")

but I am not sure that the result is what I want:

> 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, ...
Was it helpful?

Solution

Does renaming it first do what you want?

colnames(d.trainLabels) <- "V41"
cbind( d.train, d.trainLabels )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top