基于不同子网合并数据框

发布于 2025-02-13 14:46:46 字数 739 浏览 3 评论 0原文

DF <- mtcars
## subsets

df1 <- subset(DF, carb == 1)
df2 <- subset(DF, am == 1)

## table

table1 <- prop.table(with(df1, table(cyl, gear)), margin = 1)
table2 <- prop.table(with(df2, table(cyl, vs)), margin = 1)
## total

total <- cbind(table1 , table2)

这将导致:

Error in cbind(B5A2, B5A3) : 
  number of rows of matrices must match (see arg 2)

我正在尝试基于MTCARS数据集的两个不同子网(DF1,DF2)合并这两个数据帧(Table1,Table2)。到目前代码>两者都无法使用。有办法解决这个问题吗?

首选的输出应该看起来像这样:

cyl   3   4     0     1
  4 0.2 0.8 0,125 0.875
  6 1.0 0.0 1.000 0.000
  8         1.000 0.000

谢谢!

DF <- mtcars
## subsets

df1 <- subset(DF, carb == 1)
df2 <- subset(DF, am == 1)

## table

table1 <- prop.table(with(df1, table(cyl, gear)), margin = 1)
table2 <- prop.table(with(df2, table(cyl, vs)), margin = 1)
## total

total <- cbind(table1 , table2)

which results in:

Error in cbind(B5A2, B5A3) : 
  number of rows of matrices must match (see arg 2)

I'm trying to merge (preferably by cols ) those 2 data frames (table1 , table2) based on two different subets (df1, df2) from the mtcars dataset. I've tried so far: convert to data.frame and use bind.cols and plyr::rbind.fill.matrix(table1, table2) which both didn't work. Is there a way to fix this?

The preferred output should look like this:

cyl   3   4     0     1
  4 0.2 0.8 0,125 0.875
  6 1.0 0.0 1.000 0.000
  8         1.000 0.000

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2025-02-20 14:46:46

您可以从gdata软件包中使用cbindx函数:

cbindx列界带有不同数量的行。

代码:

DF <- mtcars
df1 <- subset(DF, carb == 1)
df2 <- subset(DF, am == 1)

table1 <- prop.table(with(df1, table(cyl, gear)), margin = 1)
table2 <- prop.table(with(df2, table(cyl, vs)), margin = 1)

library(gdata)
cbindX(table2, table1)
#>       0     1   3   4
#> 4 0.125 0.875 0.2 0.8
#> 6 1.000 0.000 1.0 0.0
#> 8 1.000 0.000  NA  NA

在2022-07-05创建的 reprex软件包(v2.0.1)

You could use the cbindX function from the gdata package:

cbindX column-binds objects with different number of rows.

Code:

DF <- mtcars
df1 <- subset(DF, carb == 1)
df2 <- subset(DF, am == 1)

table1 <- prop.table(with(df1, table(cyl, gear)), margin = 1)
table2 <- prop.table(with(df2, table(cyl, vs)), margin = 1)

library(gdata)
cbindX(table2, table1)
#>       0     1   3   4
#> 4 0.125 0.875 0.2 0.8
#> 6 1.000 0.000 1.0 0.0
#> 8 1.000 0.000  NA  NA

Created on 2022-07-05 by the reprex package (v2.0.1)

趁年轻赶紧闹 2025-02-20 14:46:46

您也可以用作实际tibbles /data.frames*(我正在使用Janitor's tabyl在此处使用),然后做一个完整的加入(我正在使用dplyr)。

library(dplyr)
library(janitor)

table1 <- df1 |> tabyl(cyl, gear) |> adorn_percentages()
table2 <- df2 |> tabyl(cyl, vs) |> adorn_percentages()

table1 |> full_join(table2)

输出:

 cyl   3   4     0     1
   4 0.2 0.8 0.125 0.875
   6 1.0 0.0 1.000 0.000
   8  NA  NA 1.000 0.000

(*)当您编写“ 2个数据帧(Table1,Table2)”时,您并不完全正确。您的表是表,而不是数据帧,这不是相同的。使用as.dataframe(表)将为您提供table以另一种格式组织的数据,更适合其他类型的分析。

You could also construct your frequency tables as actual tibbles/data.frames* (I am using janitor's tabyl here) and then do a full join (I am using dplyr).

library(dplyr)
library(janitor)

table1 <- df1 |> tabyl(cyl, gear) |> adorn_percentages()
table2 <- df2 |> tabyl(cyl, vs) |> adorn_percentages()

table1 |> full_join(table2)

Output:

 cyl   3   4     0     1
   4 0.2 0.8 0.125 0.875
   6 1.0 0.0 1.000 0.000
   8  NA  NA 1.000 0.000

(*) When you write "2 data frames (table1 , table2)", you're not completely right. Your tables are tables, not data frames, which is not the same. Using as.dataframe(table) will give you the data in the table organized in another format better suited for other kinds of analysis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文