我有一个植物物种的不在场矩阵,看起来像这样……
print(example.decode('utf-16'))
矩阵显示物种(1-10)是地方性的还是入侵性的,并且在某个图中是否存在(1)(0)。地块位于不同的山谷(val1,val2)
我想找出在1号山谷(val1)中发现了多少特有物种。因此,我需要知道在多个列(val1p1,val1p2,val1p3)中找到的物种总数,然后知道其中几个是特有的(即与``特有''列匹配的那些物种)。
在此示例中,山谷1中的物种总数为8,其中4种是地方性的。我想要的输出就是1号山谷中特有物种的数量!
我的真实数据集实际上非常大,我在8个不同的山谷中的75个地块中有200种,因此我需要快速的方法来做到这一点!
很显然,我无法对行进行求和,因为同一物种出现在不同的地块中。我尝试使用mutate首先将地块组合成单独的谷...
set.seed(123)
Data <- data.frame(
endemic = sample(0:1, 10, replace = TRUE),
val1p1 = sample(0:1, 10, replace = TRUE),
val1p2 = sample(0:1, 10, replace = TRUE),
val1p3 = sample(0:1, 10, replace = TRUE),
val2p1 = sample(0:1, 10, replace = TRUE),
val2p2 = sample(0:1, 10, replace = TRUE),
val2p3 = sample(0:1, 10, replace = TRUE))
Data
endemic invasive val1p1 val1p2 val1p3 val2p1 val2p2 val2p3
1 1 0 0 1 1 0 0 1
2 1 1 0 0 1 1 0 1
3 1 0 1 0 0 0 1 0
4 0 0 0 1 0 0 1 0
5 0 1 1 0 1 1 1 0
6 1 1 0 0 0 1 0 1
7 1 1 0 0 0 1 1 1
8 1 1 1 1 0 1 0 0
9 0 1 1 1 0 0 1 1
10 0 0 1 0 1 0 1 1
但是,由于不能将所有0都替换为1,因此无法正常工作。
有什么建议吗?我对R还是很陌生。
答案 0 :(得分:0)
先行一步...仔细检查!duplicate-part !!!
set.seed(123)
df <- data.frame( species = c(1:10),
endemic = sample(0:1, 10, replace = TRUE),
invasive = sample(0:1, 10, replace = TRUE),
val1p1 = sample(0:1, 10, replace = TRUE),
val1p2 = sample(0:1, 10, replace = TRUE),
val1p3 = sample(0:1, 10, replace = TRUE),
val2p1 = sample(0:1, 10, replace = TRUE),
val2p2 = sample(0:1, 10, replace = TRUE),
val2p3 = sample(0:1, 10, replace = TRUE))
df
library( tidyverse )
df %>%
gather( location, present, matches("^val") ) %>%
mutate( valley = stringr::str_extract_all( .$location, "\\d", simplify = TRUE )[,1],
p = stringr::str_extract_all( .$location, "\\d", simplify = TRUE )[,2] ) %>%
filter( valley == 1, present == 1 ) %>%
#filter out duuplicate species (from the top...) double-check if this is waht you want
filter( !duplicated( species ) )
# species endemic invasive location present valley p
# 1 1 0 0 val1p1 1 1 1
# 2 4 1 1 val1p1 1 1 1
# 3 6 0 0 val1p1 1 1 1
# 4 7 1 1 val1p1 1 1 1
# 5 8 1 0 val1p1 1 1 1
# 6 5 0 0 val1p2 1 1 2
# 7 9 1 0 val1p2 1 1 2
# 8 10 0 1 val1p3 1 1 3