C.Meadow
C.Meadow

Reputation: 33

cbind a vector of different length to a dataframe

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?

Upvotes: 3

Views: 8116

Answers (1)

akrun
akrun

Reputation: 887048

We can use cbind.fill from rowr

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

Upvotes: 5

Related Questions