使用 apply 从 R 中数据框的每一行创建一个数组?
Using apply to make an array out of each row of a dataframe in R?
我想在 R 中 运行 mantelhaen.test,这需要 3D 数组形式的 2x2 偶然事件 tables。这些 tables 可以通过遍历数据帧的每一行来构造,但我试图弄清楚是否有矢量化的方法来做到这一点 - 即使用 apply(df[c("col1"," col2",etc), margin=1, array(x, c(2,2,11))) 为 table 的每一行制作一个 3D 数组(然后将其包裹在 mantelhaen.test).
我之前已经使用 matrix() 在 R 和 Pandas 中使用 matrix() 来解决这个问题,但在这种情况下,我 运行 遇到了一个问题,其中 array()似乎对数据没有任何影响。这是一个可重现的例子:
df = data.frame(group1_variant_cases = c(2,1,3,0,0,2), group1_nonvariant_cases = c(100,92,33,40,21,87),
group1_variant_controls = c(1,2,1,0,2,1), group1_nonvariant_controls = c(45,61,70,71,31,55),
group2_variant_cases = c(0,2,1,0,1,0), group2_nonvariant_cases = c(201,99,213,52,178,98),
group2_variant_controls = c(1,0,0,0,1,2), group2_nonvariant_controls = c(67,43,12,88,91,73))
apply(head(df,1), 1, function(x) array(x, c(2,2,2)))
输出:
1
[1,] 2
[2,] 100
[3,] 1
[4,] 45
[5,] 0
[6,] 201
[7,] 1
[8,] 67
感谢任何帮助!
对于 apply
,有 simplify
个参数,默认为 TRUE
。将其更改为 FALSE
并且它有效,即根据 ?apply
If each call to FUN returns a vector of length n, and simplify is TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1. If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise.
apply(head(df,3), 1, function(x) array(x, c(2,2,2)), simplify = FALSE)
-输出
$`1`
, , 1
[,1] [,2]
[1,] 2 1
[2,] 100 45
, , 2
[,1] [,2]
[1,] 0 1
[2,] 201 67
$`2`
, , 1
[,1] [,2]
[1,] 1 2
[2,] 92 61
, , 2
[,1] [,2]
[1,] 2 0
[2,] 99 43
$`3`
, , 1
[,1] [,2]
[1,] 3 1
[2,] 33 70
, , 2
[,1] [,2]
[1,] 1 0
[2,] 213 12
我想在 R 中 运行 mantelhaen.test,这需要 3D 数组形式的 2x2 偶然事件 tables。这些 tables 可以通过遍历数据帧的每一行来构造,但我试图弄清楚是否有矢量化的方法来做到这一点 - 即使用 apply(df[c("col1"," col2",etc), margin=1, array(x, c(2,2,11))) 为 table 的每一行制作一个 3D 数组(然后将其包裹在 mantelhaen.test).
我之前已经使用 matrix() 在 R 和 Pandas 中使用 matrix() 来解决这个问题,但在这种情况下,我 运行 遇到了一个问题,其中 array()似乎对数据没有任何影响。这是一个可重现的例子:
df = data.frame(group1_variant_cases = c(2,1,3,0,0,2), group1_nonvariant_cases = c(100,92,33,40,21,87),
group1_variant_controls = c(1,2,1,0,2,1), group1_nonvariant_controls = c(45,61,70,71,31,55),
group2_variant_cases = c(0,2,1,0,1,0), group2_nonvariant_cases = c(201,99,213,52,178,98),
group2_variant_controls = c(1,0,0,0,1,2), group2_nonvariant_controls = c(67,43,12,88,91,73))
apply(head(df,1), 1, function(x) array(x, c(2,2,2)))
输出:
1
[1,] 2
[2,] 100
[3,] 1
[4,] 45
[5,] 0
[6,] 201
[7,] 1
[8,] 67
感谢任何帮助!
对于 apply
,有 simplify
个参数,默认为 TRUE
。将其更改为 FALSE
并且它有效,即根据 ?apply
If each call to FUN returns a vector of length n, and simplify is TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1. If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise.
apply(head(df,3), 1, function(x) array(x, c(2,2,2)), simplify = FALSE)
-输出
$`1`
, , 1
[,1] [,2]
[1,] 2 1
[2,] 100 45
, , 2
[,1] [,2]
[1,] 0 1
[2,] 201 67
$`2`
, , 1
[,1] [,2]
[1,] 1 2
[2,] 92 61
, , 2
[,1] [,2]
[1,] 2 0
[2,] 99 43
$`3`
, , 1
[,1] [,2]
[1,] 3 1
[2,] 33 70
, , 2
[,1] [,2]
[1,] 1 0
[2,] 213 12