在R中创建一个5行表格

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

Creating a 5-way table in R

问题

我希望创建一个5列表格,显示5个二进制变量之间的重叠计数。

类似于相关性表矩阵,但显示这些二进制变量同时等于1的次数。

因此,表格将具有列标题a、b、c、d、e和行a、b、c、d、e。

我考虑使用计数变量创建矩阵,但也愿意听取其他建议。

谢谢!

我尝试使用各种表格函数,但只能显示计数,感到困扰。

英文:

I’m hoping to create a 5-way table that displays counts of overlap between 5 binary variables.

Something like a correlation table matrix, but with counts of the number of times both of those binary variables were = 1.

So the table would have column headers a, b, c, d, e and rows a, b, c, d, and e.

I was thinking of creating a matrix using count variables, but am open to other suggestions.

Thank you!

I’ve tried using various table functions, but am struggling with only getting counts to display

答案1

得分: 4

If the columns in a data frame are binary, one can create a table of counts where two columns both have the value of one as follows.

set seed for reproducibility

set.seed(91701987)

create a data frame of 5 columns

df <- data.frame(a = runif(100),
b = runif(100),
c = runif(100),
d = runif(100),
e = runif(100))

convert to 0 / 1 values

df$a <- ifelse(df$a > .3,1,0)
df$b <- ifelse(df$b > .4,1,0)
df$c <- ifelse(df$c > .5,1,0)
df$d <- ifelse(df$d > .6,1,0)
df$e <- ifelse(df$e > .7,1,0)

Now that we've created some data, we'll use matrix multiplication to find the counts where pairs of columns both have the value of 1.

as.matrix(t(df)) %*% as.matrix(df)

...and the output:

as.matrix(t(df)) %*% as.matrix(df)
a b c d e
a 67 40 32 23 26
b 40 61 27 22 19
c 32 27 48 18 18
d 23 22 18 36 12
e 26 19 18 12 35

Now, we'll check the A / B cells (2,1) and (1,2):

check the a / b value

t(df$a) %*% df$b

...and the output, which matches the cells in the matrix above:

t(df$a) %*% df$b
[,1]
[1,] 40

英文:

If the columns in a data frame are binary, one can create a table of counts where two columns both have the value of one as follows.

# set seed for reproducibility
set.seed(91701987)

# create a data frame of 5 columna

df &lt;- data.frame(a = runif(100),
                 b = runif(100),
                 c = runif(100),
                 d = runif(100),
                 e = runif(100))

# convert to 0 / 1 values
df$a &lt;- ifelse(df$a &gt; .3,1,0)
df$b &lt;- ifelse(df$b &gt; .4,1,0)
df$c &lt;- ifelse(df$c &gt; .5,1,0)
df$d &lt;- ifelse(df$d &gt; .6,1,0)
df$e &lt;- ifelse(df$e &gt; .7,1,0)

Now that we've created some data, we'll use matrix multiplication to find the counts where pairs of columns both have the value of 1.

as.matrix(t(df)) %*% as.matrix(df)

...and the output:

&gt; as.matrix(t(df)) %*% as.matrix(df)
   a  b  c  d  e
a 67 40 32 23 26
b 40 61 27 22 19
c 32 27 48 18 18
d 23 22 18 36 12
e 26 19 18 12 35
&gt; 

Now, we'll check the A / B cells (2,1) and (1,2):

# check the a / b value
t(df$a) %*% df$b

...and the output, which matches the cells in the matrix above:

&gt; t(df$a) %*% df$b
     [,1]
[1,]   40

huangapple
  • 本文由 发表于 2023年6月9日 05:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435891.html
匿名

发表评论

匿名网友

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

确定