如何将具有不同列数的频率表进行列绑定?

huangapple go评论84阅读模式
英文:

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 &lt;- 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 &lt;- structure(c(`1` = 30L, `2` = 4L, `3` = 1L), dim = 3L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_3 &lt;- structure(c(`1` = 10L, `2` = 8L, `3` = 1L, `4` = 1L), dim = 4L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_6 &lt;- structure(c(`1` = 12L, `2` = 5L), dim = 2L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_12 &lt;- structure(c(`1` = 1L), dim = 1L, dimnames = structure(list(&quot;1&quot;), names = &quot;&quot;), class = &quot;table&quot;)

答案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 &lt;- table(mtcars$vs)
tab2 &lt;- table(mtcars$cyl)
tab3 &lt;- table(mtcars$gear)

lapply(mget(ls(pat = &quot;^tab&quot;)), stack) |&gt;
  Reduce(f = \(x, y) merge(x, y, by = &quot;ind&quot;, 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 = &quot;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 &quot;table&quot;s with unequal lengths. Essentially we add an id column and reshape.

mget(l &lt;- ls(pattern=&#39;table_&#39;)) |&gt;
  lapply(as.data.frame) |&gt;
  Map(`[&lt;-`, x=_, &#39;id&#39;, value=l) |&gt;
  suppressWarnings() |&gt;  ## *
  do.call(what=&#39;rbind&#39;) |&gt;
  reshape(direction=&#39;wide&#39;, timevar=&#39;Var1&#39;, new.row.names=l) |&gt;
  subset(select=-id) |&gt;
  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 &lt;- structure(c(`1` = 30L, `2` = 4L, `3` = 1L), dim = 3L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_3 &lt;- structure(c(`1` = 10L, `2` = 8L, `3` = 1L, `4` = 1L), dim = 4L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_6 &lt;- structure(c(`1` = 12L, `2` = 5L), dim = 2L, dimnames = structure(list( c(&quot;1&quot;, &quot;2&quot;)), names = &quot;&quot;), class = &quot;table&quot;)
table_45_12 &lt;- structure(c(`1` = 1L), dim = 1L, dimnames = structure(list(&quot;1&quot;), names = &quot;&quot;), class = &quot;table&quot;)

huangapple
  • 本文由 发表于 2023年7月24日 15:21:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定