cbind a vector of different length to a dataframe

C.Meadow picture C.Meadow · Jun 9, 2016 · Viewed 7.4k times · Source

I have a dataframe consisting of two samples. Only one sample has answered a questionnaire about state anxiety. For this case, I have calculated a vector for somatic state anxiety with the following function "rowSums":

som_lp <- rowSums(sample1[,c(1, 7, 8, 10 )+108], na.rm = TRUE)

Now I would like to add this to my existing dataframe "data", but the function "cbind" doesn't work here, because of the different lengths (dataframe 88, som_lp 59).

data <- cbind(data, som_lp) 

Can anyone help me and is there another option to calculate "som_lp" to avoid the different lengths?

Answer

akrun picture akrun · Jun 9, 2016

We can use cbind.fill from rowr

library(rowr)
cbind.fill(data, som_lp, fill = NA)