R:使用for循环将因子的级别部分匹配到字符串?

时间:2015-07-24 07:14:35

标签: r for-loop conditional-statements

我有一个地块和子图的数据集,其中我测量了树种的存在。我试图浏览数据并确定每个情节 - 子图组合中存在哪些物种。

我已经成功创建了一个数据框,用于识别每个情节 - 子图组合中存在哪些物种,但现在我正在尝试为每个物种添加指示变量(值为1)的列,以显示它们的存在。 / p>

初始代码/ data.frame看起来像这样:

f = aggregate(Species ~ Subplot + Plot, data = live.trees, 
              FUN=function(x) paste(unique(x), collapse=', '))

a=rep(0, 35)
b=cbind(a,a,a,a,a,a,a,a,a,a,a,a)
colnames(b) = levels(live.trees$Species)
freq = as.data.frame(cbind(f, b))

Species = as.factor(live.trees$Species)

#Only showing 2 of 7 plots here...

freq[1:10,]
   Subplot Plot                    Species AA AM AO BC BG BP EA RA RM SH XG XM
1        1    1                         RA  0  0  0  0  0  0  0  0  0  0  0  0
2        2    1 EA, BP, XM, BC, AA, XG, RA  0  0  0  0  0  0  0  0  0  0  0  0
3        3    1         EA, XG, AA, AM, RA  0  0  0  0  0  0  0  0  0  0  0  0
4        4    1             AA, XM, RA, EA  0  0  0  0  0  0  0  0  0  0  0  0
5        5    1             EA, BC, RA, AA  0  0  0  0  0  0  0  0  0  0  0  0
6        1    2             XM, BC, RA, AM  0  0  0  0  0  0  0  0  0  0  0  0
7        2    2                     RM, RA  0  0  0  0  0  0  0  0  0  0  0  0
8        3    2                 XM, BC, RA  0  0  0  0  0  0  0  0  0  0  0  0
9        4    2                     RA, XM  0  0  0  0  0  0  0  0  0  0  0  0
10       5    2     XM, XG, AA, BC, BG, RA  0  0  0  0  0  0  0  0  0  0  0  0

我现在正在尝试编写一个贯穿表格的for循环并粘贴" 1"如果物种的两个字符串在freq $ Species列下匹配,则在每个单独的物种列(AA,AM,AO等)中。我到目前为止制作的for循环代码是:

#Manually going through and assigning a 1 value for each species 
#using a partial string match with grepl()

    for(k in 1:nrow(freq))
  if(grepl("AA", freq$Species[[k]]) == "TRUE")
    (freq$AA[k] = 1) else
    if(grepl("AM", freq$Species[[k]]) == "TRUE")
      (freq$AM[k] = 1) else
        if(grepl("AO", freq$Species[[k]]) == "TRUE")
          (freq$AO[k] = 1) else
            if(grepl("BC", freq$Species[[k]]) == "TRUE")
              (freq$BC[k] = 1)
                  #.... etc. (cutting off here to save space)

代码在某种程度上有效,但是会覆盖每个以前的Species列,并且也非常笨重。

Subplot Plot                    Species AA AM AO BC BG BP EA RA RM SH XG XM
1        1    1                         RA  0  0  0  0  0  0  0  0  0  0  0  0
2        2    1 EA, BP, XM, BC, AA, XG, RA  1  0  0  0  0  0  0  0  0  0  0  0
3        3    1         EA, XG, AA, AM, RA  1  0  0  0  0  0  0  0  0  0  0  0
4        4    1             AA, XM, RA, EA  1  0  0  0  0  0  0  0  0  0  0  0
5        5    1             EA, BC, RA, AA  1  0  0  0  0  0  0  0  0  0  0  0
6        1    2             XM, BC, RA, AM  0  1  0  0  0  0  0  0  0  0  0  0
7        2    2                     RM, RA  0  0  0  0  0  0  0  0  0  0  0  0
8        3    2                 XM, BC, RA  0  0  0  1  0  0  0  0  0  0  0  0
9        4    2                     RA, XM  0  0  0  0  0  0  0  0  0  0  0  0
10       5    2     XM, XG, AA, BC, BG, RA  1  0  0  0  0  0  0  0  0  0  0  0

我如何:

1)获取for循环以停止覆盖先前列中的物种存在指标?

2)以更优雅的方式编写for循环?我以为我可以创建一个名为" Species"并循环遍历其中的元素(在第一个for循环中)...但是我的新手级体验开始显示。

非常感谢任何帮助或建议!

我知道这不是一个可重复的例子,但我正在寻找可能有助于我指明正确方向的一般性建议或提示。我将尝试在R中找到一个默认数据集,我可以强制同时复制我的麻烦。

提前谢谢!

注意: Species列是作为字符串创建的,因此属于类字符。

1 个答案:

答案 0 :(得分:3)

尝试

library(qdapTools)
res <- cbind(freq[1:3], mtabulate(strsplit(freq$Species, ', ')))
rowsum(res[,4:ncol(res)], group= res$Plot)
#  AA AM BC BG BP EA RA RM XG XM
#1  4  1  2  0  1  4  5  0  2  2
#2  1  1  3  1  0  0  5  1  1  4

或者

aggregate(.~Plot, res[c(2,4:ncol(res))], FUN=sum)
#   Plot AA AM BC BG BP EA RA RM XG XM
#1    1  4  1  2  0  1  4  5  0  2  2
#2    2  1  1  3  1  0  0  5  1  1  4

或者

library(dplyr)
res %>%
   group_by(Plot) %>%
   summarise_each(funs(sum), 4:ncol(res))

或者

library(data.table)
setDT(res)[, lapply(.SD, sum), by =Plot, .SDcols=4:ncol(res)]

数据

freq <- structure(list(Subplot = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L), Plot = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), Species = c("RA", 
"EA, BP, XM, BC, AA, XG, RA", "EA, XG, AA, AM, RA", "AA, XM, RA, EA", 
"EA, BC, RA, AA", "XM, BC, RA, AM", "RM, RA", "XM, BC, RA", "RA, XM", 
"XM, XG, AA, BC, BG, RA"), AA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), AM = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    AO = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), BC = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), BG = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), BP = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), EA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), RA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), RM = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SH = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), XG = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), XM = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    )), .Names = c("Subplot", "Plot", "Species", "AA", "AM", 
"AO", "BC", "BG", "BP", "EA", "RA", "RM", "SH", "XG", "XM"), 
 class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))