R:cbind 具有通用列名的数据帧
R: cbind dataframes with common column names
C 绑定两个数据帧(行数相等)和一些具有通用名称的列通常会导致 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
df2 是:
row seqnames start end width strand Whatever1 Whatever2
1 chr12 9111 9111 174 + ABC EFG
2 chr13 9112 9112 175 + ABCb EFGb
我执行 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
第二部分的值属于 df1 而不是 df2。这仅发生在 df1 和 df2 中具有相同名称的列中。它们已自动正确重命名,但它们的数据已从第一个 df 开始重复。
问题:这是正常现象吗?
希望对您有所帮助
再次感谢
不确定你的问题是什么,但你可以为每个合并对象的列指定你自己的列前缀,命名参数为 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
C 绑定两个数据帧(行数相等)和一些具有通用名称的列通常会导致 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
df2 是:
row seqnames start end width strand Whatever1 Whatever2
1 chr12 9111 9111 174 + ABC EFG
2 chr13 9112 9112 175 + ABCb EFGb
我执行 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
第二部分的值属于 df1 而不是 df2。这仅发生在 df1 和 df2 中具有相同名称的列中。它们已自动正确重命名,但它们的数据已从第一个 df 开始重复。
问题:这是正常现象吗?
希望对您有所帮助
再次感谢
不确定你的问题是什么,但你可以为每个合并对象的列指定你自己的列前缀,命名参数为 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