[英]how to merge two data frame by rows of x and y but columns should be side (df1$x) by side (df2$y)?
I have two dataframes with same name of columns and rows.我有两个具有相同名称的列和行的数据框。 I would like to merge them by rows but columns need to be side by side as of df$x and df$y.我想按行合并它们,但从 df$x 和 df$y 开始,列需要并排。
I tried so far but not getting output as required.到目前为止,我尝试过,但没有按要求获得 output。
merge(df.test1, df.test2, by.x = "V1", by.y = "V1")
Output Output
V1 2800M_15-0.5-1.x 2800M_15-0.5-2.x 2800M_15-0.5-3.x cA_15-0.5-1.x cA_15-0.5-2.x 2800M_15-0.5-1.y 2800M_15-0.5-2.y 2800M_15-0.5-3.y cA_15-0.5-1.y cA_15-0.5-2.y
1 RowA 1 1 1 1 1 1 1 1 1 1
2 RowB 1 1 1 1 1 1 1 1 1 1
3 RowC 1 1 1 1 1 1 1 1 1 1
Required Output需要 Output
V1 2800M_15-0.5-1.x 2800M_15-0.5-1.y 2800M_15-0.5-2.x 2800M_15-0.5-2.y 2800M_15-0.5-3.x 2800M_15-0.5-3.y cA_15-0.5-1.x cA_15-0.5-1.y cA_15-0.5-2.x cA_15-0.5-2.y
1 RowA 1 1 1 1 1 1 1 1 1 1
2 RowB 1 1 1 1 1 1 1 1 1 1
3 RowC 1 1 1 1 1 1 1 1 1 1
An option would be to reorder the columns based on the names ie order
on the substring of column names without the .x
or .y
at the end, and use the order index as column indexing for rearranging the columns一个选项是根据名称重新排序列,即列名的 substring 上的order
,最后没有.x
或.y
,并使用顺序索引作为列索引来重新排列列
out <- merge(df.test1, df.test2, by.x = "V1", by.y = "V1")
out1 <- out[c(1, order(sub("\\.[xy]$", "", names(out)[-1])) + 1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.