英文:
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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论