Creating a dataframe from objects of different lengths

Hi,

I'm trying to create a dataframe with various objects, to then use that as a condition to filter my larger dataframe.

Was wondering if anyone knew a way to do this, as I can't figure it out because they are of differing lengths, see reprex.

Thanks for any help.

test.patients.s1 <- c("-2147483648", "-2147483647", "-2147483636", "-2147483634")
test.patients.s2 <- c("-2147483647", "-2147483648")
test.patients.s3 <- c("-2147483639", "-2147483645", "-2147483646", "-2147483647")
test.patients.s4 <- c("-2147483648", "-2147483640")
test.patients.s5 <- c("-2147483648", "-2147483647")
test.patients.s6 <- c("-2147483648", "-2147483647", "-2147483646", "-2147483645", "-2147483644")
test.patients.s7 <- c("-2147483648", "-2147483646", "-2147483643", "-2147483647")
test.patients.s8 <- c("-2147483648", "-2147483647")
test.patients.s10 <- c("-2147483647", "-2147483646", "-2147483645", "-2147483639")
test.patients.s11 <- c("-2147483648", "-2147483633", "-2147483647")
test.patients.s15 <- c("-2147483648", "-2147483647", "-2147483646")

test.patients <- data.frame(test.patients.s1,test.patients.s2,test.patients.s3,test.patients.s4,test.patients.s5,test.patients.s6,
                      test.patients.s7,test.patients.s8,test.patients.s10,test.patients.s11,test.patients.s15)

I'm sure there are more elegant solutions but try the ldply function from plyr :

library(plyr)
my_list <- list(test.patients.s1,test.patients.s2,test.patients.s3,test.patients.s4,test.patients.s5,test.patients.s6,
                test.patients.s7,test.patients.s8,test.patients.s10,test.patients.s11,test.patients.s15)

test.patients <- as.data.frame(t(ldply(my_list, rbind)))
colnames(test.patients) <- c("test.patients.s1","test.patients.s2","test.patients.s3","test.patients.s4","test.patients.s5","test.patients.s6",
                    "test.patients.s7","test.patients.s8","test.patients.s10","test.patients.s11","test.patients.s15")

Works perfectly, thanks for the help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.