我想使用 apply 语句对 R 中数据帧的每一行执行某些操作。
以下工作是我使用一堆参数和索引 i 调用函数“calc.Sphere.Metrics”。我将结果存储在每一行中。
for(i in 1: dim(position.matrix)[1]){
results.obs[i,] <- calc.Sphere.Metrics(i, culled.mutation.data, position.matrix, protein.metrics, radius)
}
我尝试了几种 apply、mapply 语句,但没有运气。执行此操作的正确方法是什么?
编辑: 根据要求,这是 calc.Sphere.Metrics 的骨架
calc.Sphere.Metrics <- function(index, culled.mutation.data, position.matrix, protein.metrics, radius){
results <- matrix(data = 0, nrow = 1, ncol = 8)
colnames(results) <- c("Line.Length","Center", "Start","End","Positions","MutsCount","P.Value", "Within.Range")
results <- as.data.frame(results)
....
look up a bunch of stuff and fill in each column of results. All the data required is in the parameters passed in and the index.
.....
return(results)
}
Results 与 top 函数中的 results.obs 具有相同的列数。希望这有帮助!
谢谢!
最佳答案
大概是这样的:
result.obs <- do.call(rbind, lapply(seq_len(dim(position_matrix)[1]),
calc.Sphere.Metrics, culled.mutation.data, position.matrix, protein.metrics, radius))
关于r - R中的apply语句有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993940/