我有两个数据框,它们看起来像:
df <- data.frame(
A = c(19, 79, 34),
B = c(21, 21, 14),
C = c(17, 14, 57)
)
row.names(df) <- c("2016-10-31", "2016-09-30", "2016-08-31")
df
A B C
2016-10-31 19 21 17
2016-09-30 79 21 14
2016-08-31 34 14 57
df2 <- data.frame(
A_Return = c(0.1, 0.9, 0.4),
B_Return = c(0.1, 0.2, 0.4),
C_Return = c(0.7, 0.4, 0.7)
)
row.names(df2) <- c("2016-10-31", "2016-09-30", "2016-08-31")
df2
A_Return B_Return C_Return
2016-10-31 0.1 0.1 0.7
2016-09-30 0.9 0.2 0.4
2016-08-31 0.4 0.4 0.7
数据框具有相同的行名称,我知道cbind
或merge
可以将它们组合在一起。但我希望列按以下顺序排列:
A A_Return B B_Return C C_Return
2016-10-31 19 0.1 21 0.1 17 0.7
2016-09-30 79 0.9 21 0.2 14 0.4
2016-08-31 34 0.4 14 0.4 57 0.7
答案 0 :(得分:1)
我们可以merge
row.names
dfN <- merge(df, df2, by = "row.names")
row.names(dfN) <- dfN$Row.names
dfN1 <- dfN[-1][order(colnames(dfN)[-1])]