0

只有当我的数据框的列名(即物种)与第二个属性表中的两个参数匹配时,我才想计算行和。这意味着它应该首先匹配属性表的一列中的名称,并且在属性表的另一列中具有某个条目。但是,属性表包含比原始数据框更多的物种。

我试过 :

# Species data from vegan package:
data(varespec, package = "vegan")

# create attributes table
attributes <- matrix(NA, length(varespec), 2)
attributes[,1] <- colnames(varespec)
attributes[,2] <- c(rep("MI",14),rep("PI",30))

# add species to the attribute table
x <- c("spec1","MI")
y <- c("spec2","PI")

attributes <- rbind(attributes, x, y)
row.names(attributes) <- c(1:46)

# calculate rowsums only for species contained in the attributes table 
# and having the entry "MI" in the attributes table
for (i in 1:44){
  for (j in 1:46){
    if ((colnames(varespec)[i] == attributes[j,1]) & (attributes[j,2] == "MI")) {
      apply(varespec,1,sum)
    }
  }}

但它总是总结整行,而不仅仅是 MI - 物种。

4

1 回答 1

0

如果将数据集转换为长格式,这很容易解决

library(dplyr)
library(tidyr)
data(varespec, package = "vegan")
attributes <- data.frame(
  Species = c(colnames(varespec), "spec1", "spec2"),
  Attribute = c(rep(c("MI", "PI"), c(14, 30)), "MI", "PI")
)
varespec %>% 
  add_rownames("ID") %>% 
  gather(Species, Value, -ID) %>% #convert to long format
  inner_join(attributes, by = "Species") %>% 
  filter(Attribute == "MI") %>% 
  group_by(ID) %>% 
  summarise(Total = sum(Value))
于 2015-10-07T13:57:36.577 回答