简体   繁体   English

使用带有多个变量的lapply()

[英]using lapply() with multiple variables

I've got a cross-tab frequency table where the measure is CAG and columns A01, A02 etc are frequency counts. 我有一个交叉表频率表,其中的度量是CAG,列A01,A02等是频率计数。 ie 6485 counts of 13 CAG, 35 counts of CAG 14. I want to sum the values in each column, provided the CAG for that row is greater than or equal to the modal CAG value. 即6485计数为13 CAG,35计数为CAG14。我想对每一列的值求和,只要该行的CAG大于或等于模态CAG值。 Then I will divide that by the sum of A01. 然后我将其除以A01的总和。 This provides me the proportion of values that are greater than or equal to the mode. 这为我提供了大于或等于该模式的值的比例。 I've managed to get it working for one column, but I want to run it over each column, using the relevant mode for each column. 我设法使它适用于一列,但我想使用每一列的相关模式在每一列上运行它。 I'd appreciate any help! 我将不胜感激!

data <- data.frame(CAG = c(13, 14, 15, 17), 
                   A01 = c(6485,35,132, 12), 
                   A02 = c(0,42,56, 4))

mode <- data$CAG[data$A01 == max(data$A01)]

B <- lapply(data[, 2:ncol(data)], function(x) {
    sum(x[data$CAG >= mode])
})

prop <- B / sum(data$A01)

You need to put the mode calculation in the function too. 您还需要将模式计算放入功能中。

sapply(data[, 2:ncol(data)], function(x) {
  mode <- data$CAG[which.max(x)]
  B <- sum(x[data$CAG >= mode])
  B/sum(x)
})
##       A01       A02 
## 1.0000000 0.5882353 

The function which.max is equivalent (at least in this use) to x==max(x) . 函数which.max等效于(至少在此使用中) x==max(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM