# Interweave in dataframe
# Useful Link: http://stackoverflow.com/questions/14784861/interweave-two-data-frames-in-r
a = data.frame(x=1:5, y=5:1)
b = data.frame(x=2:6, y=4:0)
# Interweave rows
df <- rbind(data.frame(a, index = 1:nrow(a)), data.frame(b, index = 1:nrow(b)))
df <- df[order(df$index), c("x", "y")]
# Interweave columns
dfc <- cbind(rbind(a, 1:ncol(a)), rbind(b, 1:ncol(b)))
dfc[order(dfc[6,])]
# Try: interweave columns using names of the columns
dfc[order(names(dfc))] # This is faster and single line call!
沒有留言:
張貼留言