简体   繁体   English

如何按 x 和 y 行合并两个数据框,但列应该并排(df1$x)(df2$y)?

[英]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.

相关问题 创建基于DF2超过DF1的数据框,具有相同的行和行(特殊条件合并) - Create a Data frame based on DF2 exceeding DF1 with the same rows and line (special merge with condition) 如何创建一个新的数据框“df2”,它汇总了数据框“df1”的行数,但以日期时间为条件 - How to create a new data frame `df2` that summarises the number of rows of a data frame `df1` but conditioned to the DateTime 如何在df1中添加基于df2的两个CONSECUTIVE列之间的间隔进行插值的新列(df2 $`5`,df2 $`15`,df2 $`25`,df2 $`35`) - How to add a new column in df1 that is an interpolation based on intervals between two CONSECUTIVE columns of df2 (df2$`5`,df2$`15`,df2$`25`,df2$`35`) 在R中分配df $ COLUMN [x] = df2 $ COLUMN [y]的问题 - Problem with assign df$COLUMN[x] = df2$COLUMN[y] in R r在df1中添加df2中的行数(条件) - r add columns in df1 with count of rows in df2 (conditional) 比较两个 dataframe 中的列,并在匹配时将 df2 中的行 cbind 到 df1 - Compare columns from two dataframe and cbind the rows from df2 to df1 when matching 仅选择df2中存在列组合的df1行的简单方法(不加入df1,df2) - simple way to select only rows of df1 where combination of columns is present in df2 (NOT joining df1, df2) 如何删除 df2 中存在的 df1 中的行? - how to remove rows in df1 present in df2? 当`df1$DateTime==df2$DateTime`时,如何将`DateTime`从`df1`更改为`DateTime2`从`df2`。 对于 df1 的其余行,我减去 60s - How to change `DateTime` from `df1` to `DateTime2` from `df2` when `df1$DateTime==df2$DateTime`. For the rest of rows of `df1` I subtract 60s 如果 df1 中的字符串值“X”等于 df2 中的任何字符串值,则将类别“1”分配给 R 中 df1 中新列中的值 X - If string value “X” in df1 is equal to any of the string values in df2, assign category “1” to value X in a new column in df1 in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM