在 R 中合并表格
Combining Tables In R
已更新
我在尝试在 R 中执行简单的 table 合并任务时遇到问题。我正在寻找发生这种情况的原因,如果存在的话,还有更优雅的解决方案。以下是我正在使用的确切数据以及发生的情况。
我有两个 table 来自与不同政党和商界人士如何看待政治问题的调查。它们来自两个不同的数据集,我不想合并它们(出于各种原因)。每个数据都具有完全相同的值名称。但是,当我使用 cbind
合并列时,某些列的行会反转,我不明白为什么。
我的数据
library(survey)
founders.services <-
structure(c(38, 43, 131, 172, 177, 122, 34, 12, 114, 70, 17,
27), .Dim = c(2L, 6L), .Dimnames = structure(list(services = c("compete",
"similar"), party = c("skipped", "Democrat", "Independent", "Libertarian",
"Republican", "other")), .Names = c("services", "party")))
public.services.party <-
structure(c(26, 103), .Dim = 2L, .Dimnames = structure(list(services = c("similar",
"compete")), .Names = "services"), class = c("svytable", "xtabs",
"table"), call = svytable.survey.design(formula = ~services,
design = fss))
以及,我如何组合它们:
cbind(founders.services, public.services.party)
在第一个(也是正确的)table 中,在列 "libertarian" 下,行 "compete" 的值为 34,而 "similar" 的值为 12。但是,在组合 table(与 cbind)中,情况正好相反。如果名称不同,它们应该显示为不同的列。但是,cbind 似乎认识到它们都是相同的值。
为什么会这样?
而且,更一般地说,如果有更好的方法来组合 table,我很乐意考虑替代方案。基本上我拥有的是一些不同的数据集,其中不同的人群(政党、企业类型)回答了相同的问题。我无法合并数据集,但想合并 table 进行分析。
谢谢,如果我能把这个问题说得更清楚,请告诉我。
更新:带有代码和 tables.
这是正确的table
services skipped Democrat Independent Libertarian Republican other
compete 38 131 177 34 114 17
similar 43 172 122 12 70 27
这是合并后的 table,有错误。您将需要 "survey" 包来复制。
founders.services skipped Democrat Independent Libertarian Republican other
similar 26 38 131 177 34 114 17
compete 103 43 172 122 12 70 27
它正在从您放入 cbind 命令的第一个数据框中获取行名称。如果你只是颠倒cbind的顺序,你会得到你想要的:
> cbind( public.services.party,founders.services)
skipped Democrat Independent Libertarian Republican other founders.services
compete 38 131 177 34 114 17 26
similar 43 172 122 12 70 27 103
之后您可以根据需要重新排列列和行。
这是我提到的另一种方式,使用函数来匹配行号。它依赖于转换为数据帧,但不确定这是否可以接受。
您还需要解决结果数据框第一列的名称更改问题。
library(survey)
fs <- structure(
c(38, 43, 131, 172, 177, 122, 34, 12, 114, 70, 17, 27),
.Dim = c(2L, 6L),
.Dimnames = structure(list(services = c("compete", "similar"),
party = c("skipped", "Democrat", "Independent",
"Libertarian", "Republican", "other")),
.Names = c("services", "party")))
psp <- structure(c(26, 103),
.Dim = 2L,
.Dimnames = structure(list(services = c("similar", "compete")),
.Names = "services")
# ,class = c("svytable", "xtabs", "table")
# , call = svytable.survey.design(formula = ~services,
# design = fss)
)
cbind(fs, psp)
cbind( psp,fs)
mergeByRowName <- function(d1,d2){
d1 <- data.frame(d1)
d2 <- data.frame(d2)
d1$rn <- rownames(d1)
d2$rn <- rownames(d2)
d3 <- merge(d1,d2,by="rn")
rownames(d3) <- d3$rn
d3$rn <- NULL
return(d3)
}
d3 <- mergeByRowName(fs,psp)
d3
产生这个:
skipped Democrat Independent Libertarian Republican other d2
compete 38 131 177 34 114 17 103
similar 43 172 122 12 70 27 26
已更新
我在尝试在 R 中执行简单的 table 合并任务时遇到问题。我正在寻找发生这种情况的原因,如果存在的话,还有更优雅的解决方案。以下是我正在使用的确切数据以及发生的情况。
我有两个 table 来自与不同政党和商界人士如何看待政治问题的调查。它们来自两个不同的数据集,我不想合并它们(出于各种原因)。每个数据都具有完全相同的值名称。但是,当我使用 cbind
合并列时,某些列的行会反转,我不明白为什么。
我的数据
library(survey)
founders.services <-
structure(c(38, 43, 131, 172, 177, 122, 34, 12, 114, 70, 17,
27), .Dim = c(2L, 6L), .Dimnames = structure(list(services = c("compete",
"similar"), party = c("skipped", "Democrat", "Independent", "Libertarian",
"Republican", "other")), .Names = c("services", "party")))
public.services.party <-
structure(c(26, 103), .Dim = 2L, .Dimnames = structure(list(services = c("similar",
"compete")), .Names = "services"), class = c("svytable", "xtabs",
"table"), call = svytable.survey.design(formula = ~services,
design = fss))
以及,我如何组合它们:
cbind(founders.services, public.services.party)
在第一个(也是正确的)table 中,在列 "libertarian" 下,行 "compete" 的值为 34,而 "similar" 的值为 12。但是,在组合 table(与 cbind)中,情况正好相反。如果名称不同,它们应该显示为不同的列。但是,cbind 似乎认识到它们都是相同的值。
为什么会这样?
而且,更一般地说,如果有更好的方法来组合 table,我很乐意考虑替代方案。基本上我拥有的是一些不同的数据集,其中不同的人群(政党、企业类型)回答了相同的问题。我无法合并数据集,但想合并 table 进行分析。
谢谢,如果我能把这个问题说得更清楚,请告诉我。
更新:带有代码和 tables.
这是正确的table
services skipped Democrat Independent Libertarian Republican other
compete 38 131 177 34 114 17
similar 43 172 122 12 70 27
这是合并后的 table,有错误。您将需要 "survey" 包来复制。
founders.services skipped Democrat Independent Libertarian Republican other
similar 26 38 131 177 34 114 17
compete 103 43 172 122 12 70 27
它正在从您放入 cbind 命令的第一个数据框中获取行名称。如果你只是颠倒cbind的顺序,你会得到你想要的:
> cbind( public.services.party,founders.services)
skipped Democrat Independent Libertarian Republican other founders.services
compete 38 131 177 34 114 17 26
similar 43 172 122 12 70 27 103
之后您可以根据需要重新排列列和行。
这是我提到的另一种方式,使用函数来匹配行号。它依赖于转换为数据帧,但不确定这是否可以接受。
您还需要解决结果数据框第一列的名称更改问题。
library(survey)
fs <- structure(
c(38, 43, 131, 172, 177, 122, 34, 12, 114, 70, 17, 27),
.Dim = c(2L, 6L),
.Dimnames = structure(list(services = c("compete", "similar"),
party = c("skipped", "Democrat", "Independent",
"Libertarian", "Republican", "other")),
.Names = c("services", "party")))
psp <- structure(c(26, 103),
.Dim = 2L,
.Dimnames = structure(list(services = c("similar", "compete")),
.Names = "services")
# ,class = c("svytable", "xtabs", "table")
# , call = svytable.survey.design(formula = ~services,
# design = fss)
)
cbind(fs, psp)
cbind( psp,fs)
mergeByRowName <- function(d1,d2){
d1 <- data.frame(d1)
d2 <- data.frame(d2)
d1$rn <- rownames(d1)
d2$rn <- rownames(d2)
d3 <- merge(d1,d2,by="rn")
rownames(d3) <- d3$rn
d3$rn <- NULL
return(d3)
}
d3 <- mergeByRowName(fs,psp)
d3
产生这个:
skipped Democrat Independent Libertarian Republican other d2
compete 38 131 177 34 114 17 103
similar 43 172 122 12 70 27 26