我想使用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)
}
我已经尝试了几个申请,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)
}
结果与top函数中的results.obs具有相同的列数。希望这有帮助!
谢谢!
答案 0 :(得分:3)
可能是这样的:
result.obs <- do.call(rbind, lapply(seq_len(dim(position_matrix)[1]),
calc.Sphere.Metrics, culled.mutation.data, position.matrix, protein.metrics, radius))