stack_df {lazy.tools}R Documentation

Vertically Stack Data Frames with Different Sets of Variables

Description

Vertically Stack Data Frames with Different Sets of Variables

Usage

stack_df(..., sort = 0)

Arguments

...

data frames

sort

= 1 to alphabetically sort the variable result
Otherwise, the order of the original data frames will be kept.

Details

Using the data frames in the example section, the core part is:
res <- merge( data.frame(df1m,id=1:nrow(df1m)) , data.frame(df2m,id=(1:nrow(df2m))+nrow(df1m)) ,all=1)
res <- res[order(res$id),]

Note that, since check.names=0 and optional=1 are used when creating data frames, the resulting data frame may not have valid column names.

When all the row names are unique, it will be used as the row names of the resulting data frame. Otherwise, 1:nrow(result) will be used.

Value

A data frame

Examples

A <- demomat(5,4,3,cnroot="V")
A1 <- A[,,1]; A2 <- A[,,2]; A3 <- A[,,3]
A1m <- A1[,c(1,4)]; A2m <- A2[,c(1,3)]; A3m <- A3[,c(4,2,3)];
df1m <- as.data.frame(A1m)
df2m <- as.data.frame(A2m)
df3m <- as.data.frame(A3m)

res <- stack_df( df1m, df2m, df3m )
Print(A1m,A2m,A3m)
Print(res)
res2 <- stack_df( df2m, df1m, df3m )
Print(res2)
res3 <- stack_df( df1m, df2m, df3m, sort=1 )
Print(res3)

res4 <- stack_df( A=demomat(3,2), b=letters[1:3] )
Print(res4)


[Package lazy.tools version 0.1.3 Index]