r - R中的apply语句有问题

标签 r apply

我想使用 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/

相关文章:

r - 将函数应用于数据框列(dplyr)

r - 如何cbind R中矩阵列表的所有行组合

r - 在 R 中运行回归循环的最佳方法是什么?

r - 将多个功能应用于数据框的每一行

每个人重复一个模型 100 次

r - R 中具有对数轴的散点图矩阵

r - 使用数据库时 dplyr 加入 : How do you do a non standard join `col1` ! = `col2`?

r - 更新了 R "maps"包的世界地图?

r - 在 R 中可视化 2 变量联合概率质量函数

save - 保存和应用按钮之间的区别?