小编Jul*_*ien的帖子

我如何获得列联表?

我正在尝试从特定类型的数据创建列联表.这对于循环等是可行的......但是因为我的最终表将包含超过10E5的单元格,所以我正在寻找一个预先存在的函数.

我的初步数据如下:

PLANT                  ANIMAL                          INTERACTIONS
---------------------- ------------------------------- ------------
Tragopogon_pratensis   Propylea_quatuordecimpunctata         1
Anthriscus_sylvestris  Rhagonycha_nigriventris               3
Anthriscus_sylvestris  Sarcophaga_carnaria                   2
Heracleum_sphondylium  Sarcophaga_carnaria                   1
Anthriscus_sylvestris  Sarcophaga_variegata                  4
Anthriscus_sylvestris  Sphaerophoria_interrupta_Gruppe       3
Cerastium_holosteoides Sphaerophoria_interrupta_Gruppe       1
Run Code Online (Sandbox Code Playgroud)

我想创建一个这样的表:

                       Propylea_quatuordecimpunctata Rhagonycha_nigriventris Sarcophaga_carnaria Sarcophaga_variegata Sphaerophoria_interrupta_Gruppe
---------------------- ----------------------------- ----------------------- ------------------- -------------------- -------------------------------
Tragopogon_pratensis   1                             0                       0                   0                    0
Anthriscus_sylvestris  0                             3                       2                   4                    3
Heracleum_sphondylium  0                             0                       1                   0                    0
Cerastium_holosteoides 0                             0                       0                   0                    1
Run Code Online (Sandbox Code Playgroud)

也就是说,行中的所有植物物种,列中的所有动物物种,有时没有相互作用(而我的初始数据仅列出发生的相互作用).

r contingency

23
推荐指数
5
解决办法
6万
查看次数

如何在R中的列表中组合不同长度的向量?

组合列表中包含的以下向量时遇到问题:

x <- list(as.numeric(c(1,4)),as.numeric(c(3,19,11)))
names (x[[1]]) <- c("species.A","species.C")
names (x[[2]]) <- c("species.A","species.B","species.C")
Run Code Online (Sandbox Code Playgroud)

给出以下列表:

>x
>[[1]]
>species.A species.C 
>         1         4 
>[[2]]
>species.A species.B species.C 
>        3        19        11 

使用do.call函数组合它们: y<- do.call(cbind,x)

得到:

>y
>             [,1] [,2]
>   species.A    1    3
>   species.B    4   19
>   species.C    1   11

虽然我想获得这个:

>             [,1] [,2]
>   species.A    1    3
>   species.B   NA   19
>   species.C    4   11

r list numeric do.call

6
推荐指数
2
解决办法
8595
查看次数

标签 统计

r ×2

contingency ×1

do.call ×1

list ×1

numeric ×1