Search code examples
rdplyrcbind

How to use cbind to transfer 2 rows from one df into another in a 300+ column dataset


I have 2 datasets, one has numerical variables and one has character variables, from df1.

In the character df I want to add Respondent.ID from df1. In the numerical df, I want to add Column ColumnName1 and ColumnName2 from df1.

My cbind() is not working - when I run the code, it basically gives me the original dataframe, df1:

numericaldata <- dplyr::select_if(binarydata, is.numeric)
characterdata <- dplyr::select_if(binarydata, is.character)

characterdata <- cbind(binarydata,share = "Respondent.ID"])
numericaldata <- cbind(binarydata, share = "SectorCollapsed", "location")

Any help appreciated, thank you kindly!


Solution

  • Your cbind() call is off. What it will do now is bind the columns of binarydata and one column called share with the same amount of rows as binarydata and with value "Respondent.ID" in every row.

    Furthermore, when using cbind() you should pass along all the dataframes or columns that you would like to combine. A solution would be

    characterdata <- cbind(characterdata, binarydata["Respondent.ID"])
    numericaldata <- cbind(numericaldata, binarydata[, c("SectorCollapsed, location")])