从2'换循环'切换到'外'时'未定义列被选中'

时间:2015-09-29 19:32:01

标签: r dataframe

我有2个表:freq.table具有每个地区的类型分布,match.table表示哪些类型匹配

#freq.table               #match.table
       typeA  typeB   |         typeA typeB  
aland   30.0   70.0   |   typeA  TRUE  TRUE
bland   70.0   30.0   |   typeB  TRUE FALSE

现在我想要一个像freq.table这样的新表但具有不同的值:
对于freq(国家/地区,类型)中的每个点,新值应为all(country,type_m)的总和,其中(type,type_m)在match.table中为TRUE。
也可以说我要合并freq[country,]match[type,] 匹配类型的频率,但仅匹配matchTRUE的列 所以在matches我存储匹配类型,freq[country,matches]为我提供了所需的所有频率。

match_table = read.table(file='donor_for.table', header=T, stringsAsFactors = FALSE)
freq        = read.table(file='frequency.table', header=T, stringsAsFactors = FALSE)

fill_table <- function(FUN){     
  newfr <-freq
  for (country in rownames(freq)){
    for (type in colnames(freq)){
      newfr[country, type] <- FUN(country, type)
    }
  }
  return(newfr)
}

fill_table2 <- function(FUN){    
  newfr <-freq
  newfr[] <- outer(rownames(freq), colnames(freq), FUN=FUN)
  return(newfr)
}

find_donor <- function(country, type_receiver) {
  matches=colnames(match_table)[match_table[type_receiver,]==TRUE]
  return( sum(freq[country,matches]) )    #24
}

虽然fill_table(find_donor)有效,但fill_table2(find_donor)(应该做同样但更简洁)会产生:

 Error in `[.data.frame`(freq, country, matches) : 
  undefined columns selected 
6 stop("undefined columns selected") 
5 `[.data.frame`(freq, country, matches) at blood.r#24
4 freq[country, matches] at blood.r#24
3 FUN(X, Y, ...) 
2 outer(rownames(freq), colnames(freq), FUN = FUN) at blood.r#18
1 fill_table2(find_donor)

0 个答案:

没有答案