英文:
How to column-bind frequency tables with different column numbers?
问题
我有一个频率表,其中包含不同的列数。我正在使用以下代码,但结果不准确。
data_cbind <- cbind(table_45_1, table_45_3, table_45_6, table_45_12)
我想要将数据组织如下:
数据
table_45_1 <- structure(c(`1` = 30L, `2` = 4L, `3` = 1L), dim = 3L, dimnames = structure(list( c("1", "2", "3")), names = ""), class = "table")
table_45_3 <- structure(c(`1` = 10L, `2` = 8L, `3` = 1L, `4` = 1L), dim = 4L, dimnames = structure(list( c("1", "2", "3", "4")), names = ""), class = "table")
table_45_6 <- structure(c(`1` = 12L, `2` = 5L), dim = 2L, dimnames = structure(list( c("1", "2")), names = ""), class = "table")
table_45_12 <- structure(c(`1` = 1L), dim = 1L, dimnames = structure(list("1"), names = ""), class = "table")
英文:
I have a frequency table that has different column numbers. I am using the following code but it is not resulting in accurate results.
data_cbind <- cbind(table_45_1, table_45_3, table_45_6 table_45_12)
head(table_45_1)
1 2 3
30 4 1
head(table_45_3)
1 2 3 4
10 8 1 1
head(table_45_6)
1 2
12 5
head(table_45_12)
1
1
I want to organize the data as follows:
data
table_45_1 <- structure(c(`1` = 30L, `2` = 4L, `3` = 1L), dim = 3L, dimnames = structure(list( c("1", "2", "3")), names = ""), class = "table")
table_45_3 <- structure(c(`1` = 10L, `2` = 8L, `3` = 1L, `4` = 1L), dim = 4L, dimnames = structure(list( c("1", "2", "3", "4")), names = ""), class = "table")
table_45_6 <- structure(c(`1` = 12L, `2` = 5L), dim = 2L, dimnames = structure(list( c("1", "2")), names = ""), class = "table")
table_45_12 <- structure(c(`1` = 1L), dim = 1L, dimnames = structure(list("1"), names = ""), class = "table")
答案1
得分: 4
你可以创建一个表格列表,然后使用 stack
函数对它们进行堆叠,最后使用 Reduce
函数将它们合并在一起:
#模拟数据
tab1 <- table(mtcars$vs)
tab2 <- table(mtcars$cyl)
tab3 <- table(mtcars$gear)
lapply(mget(ls(pat = "^tab")), stack) %>%
Reduce(f = \(x, y) merge(x, y, by = "ind", all = TRUE))
为了获得更漂亮的输出,你甚至可以使用 as.data.frame.table
而不是 stack
,它具有一个 responseName
参数(请参阅 as.data.frame(tab1, responseName = "cyl")
)。
英文:
You can create a list of tables, then stack
them, and use Reduce
to merge them altogether:
#Mock data
tab1 <- table(mtcars$vs)
tab2 <- table(mtcars$cyl)
tab3 <- table(mtcars$gear)
lapply(mget(ls(pat = "^tab")), stack) |>
Reduce(f = \(x, y) merge(x, y, by = "ind", all = TRUE))
To get a nicer output, you can even use as.data.frame.table
instead of stack
which has a responseName
argument (see as.data.frame(tab1, responseName = "cyl)
).
答案2
得分: 1
以下是代码的翻译部分:
这里有一种方法可以合并具有不等长度的频率表 "table"。基本上,我们添加一个 "id" 列并进行重塑。
mget(l <- ls(pattern='table_')) |>
lapply(as.data.frame) |>
Map(`[<-`, x=_, 'id', value=l) |>
suppressWarnings() |>
do.call(what='rbind') |>
reshape(direction='wide', timevar='Var1', new.row.names=l) |>
subset(select=-id) |>
t()
# table_45_1 table_45_12 table_45_3 table_45_6
# Freq.1 30 1 10 12
# Freq.2 4 NA 8 5
# Freq.3 1 NA 1 NA
# Freq.4 NA NA 1 NA
关于警告,请参阅 这里。
数据部分不需要翻译。
英文:
Here's a way to rbind
frequency "table"
s with unequal lengths. Essentially we add an id
column and reshape
.
mget(l <- ls(pattern='table_')) |>
lapply(as.data.frame) |>
Map(`[<-`, x=_, 'id', value=l) |>
suppressWarnings() |> ## *
do.call(what='rbind') |>
reshape(direction='wide', timevar='Var1', new.row.names=l) |>
subset(select=-id) |>
t()
# table_45_1 table_45_12 table_45_3 table_45_6
# Freq.1 30 1 10 12
# Freq.2 4 NA 8 5
# Freq.3 1 NA 1 NA
# Freq.4 NA NA 1 NA
*As for the warning, see here.
Data:
table_45_1 <- structure(c(`1` = 30L, `2` = 4L, `3` = 1L), dim = 3L, dimnames = structure(list( c("1", "2", "3")), names = ""), class = "table")
table_45_3 <- structure(c(`1` = 10L, `2` = 8L, `3` = 1L, `4` = 1L), dim = 4L, dimnames = structure(list( c("1", "2", "3", "4")), names = ""), class = "table")
table_45_6 <- structure(c(`1` = 12L, `2` = 5L), dim = 2L, dimnames = structure(list( c("1", "2")), names = ""), class = "table")
table_45_12 <- structure(c(`1` = 1L), dim = 1L, dimnames = structure(list("1"), names = ""), class = "table")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论