R: cbind 数据框与公共列名

IVy*_*IVy -2 r cbind

将两个数据帧(行数相等)与几个具有通用名称的列绑定在一起通常会导致 data.frame 具有更改的通用名称(例如 NameA.1、NameB.1 等)以避免任何问题。

我注意到即使名称已更改,也有数据替换。具体来说,生成的 data.frame 在所有具有相同名称的列中包含来自第一个 data.frame 的数据,即使在那些应该具有来自第二个 data.frame 的数据的列中也是如此。

这个很容易克服,因为可以在 cbind 之前更改名称,但它可能会在结果中隐藏错误。

------编辑---- 我会尝试提供一个例子:

df1 是:

    row     seqnames    start   end     width   strand  Region  Extra1
    1       chr10       8111    8111    172      *      123      456
    2       chr11       8112    8112    173      *      123b     456b
Run Code Online (Sandbox Code Playgroud)

和 df2 是:

    row     seqnames    start   end     width   strand  Whatever1 Whatever2
    1       chr12       9111    9111    174      +      ABC      EFG
    2       chr13       9112    9112    175      +      ABCb     EFGb
Run Code Online (Sandbox Code Playgroud)

我执行 cbind 并得到:

    row     seqnames    start   end     width   strand  Region  Extra1  seqnames.1  start.1 end.1   width.1 strand.1 Whatever1 Whatever2
    1       chr10       8111    8111    172      *      123      456    chr10       8111    8111    172      *        ABC        EFG
    2       chr11       8112    8112    173      *      123b     456b   chr11       8112    8112    173      *        ABCb       EFGb
Run Code Online (Sandbox Code Playgroud)

第二部分中的值属于 df1 而不是 df2。这仅发生在 df1 和 df2 中具有相同名称的列中。它们已自动正确重命名,但它们的数据已从第一个 df 重复。

问题:这是正常行为吗?

我希望这有帮助

再次感谢你

cyb*_*j0g 7

不确定您的问题是什么,但您可以为每个合并对象的列指定自己的列前缀,命名参数为cbind

data('cars')
cars2=cbind(DataSet1=cars, DataSet2=cars)
head(cars2)
# DataSet1.speed DataSet1.dist DataSet2.speed DataSet2.dist
# 1              4             2              4             2
# 2              4            10              4            10
# 3              7             4              7             4
# 4              7            22              7            22
# 5              8            16              8            16
# 6              9            10              9            10
Run Code Online (Sandbox Code Playgroud)