【问题标题】:read the variable from a matrix and use them in a function in R?从矩阵中读取变量并在 R 中的函数中使用它们?
【发布时间】:2015-07-03 03:24:51
【问题描述】:

我有一个名为 "met" 的 682*1 矩阵,前 5 行如下所示:

>  rownames(met)[1:5]

    [1]  "glycine_imp" 
    [2]  "Nacetylglycine_imp" 
    [3]  "sarcosine_imp"      
    [4]  "dimethylglycine_imp" 
    [5]  "betaine_imp" 

我在 R 中有一个函数,每次使用“met”矩阵的行名之一并创建一个新的对象文件,我应该保存对象!

my function is  
prep[i]<- Scores(Z=GT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") 

每次我应该更改met [i]并将其替换为“met”矩阵的行名(一个接一个),对于每个人我应该更改函数的名称,然后我应该保存每个对象!

例如对于“met”的第一行,我有“glycine_imp”

>   **prep1**<- Scores (Z=GT,formula="**glycine_imp**~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") 
    #creat the object file for first row and called prep1###

>   save(prep1, file="prep1.RData", compress="bzip2")
    ##save the object file as "prep1.RData"#####

我应该对“met”矩阵的 682 行名称执行此过程,最后我应该有
"prep1.RData" , "prep2.RData" , "prep3.RData"

我使用循环功能,但它有不同的错误!! 请问我该怎么办?

非常感谢

【问题讨论】:

  • 这一点都不清楚。您似乎认为met[i] 应该以某种方式引用行名?这将要求我们能够查看您的 Scores 函数中的代码以提供任何建议。我将推迟投票结束,但如果你不解决缺乏明确性的问题,我将在一个小时左右投票结束。
  • 你也不应该把帖子发给 SO 和 r-help。

标签: r


【解决方案1】:

我不知道你的功能的细节,所以我只能提供一个大致的想法。

A <- matrix(1:10, ncol = 5)
Scores <- function(x){return(x+1)}

for(i in 1:10){
  df <- data.frame(tmp = 1)
  name <- paste("prep", as.character(i), sep = "") 
  df[[name]]=Scores(A)
  save(file = paste("prep", as.character("i"), ".RData", sep = ""), df[[name]], 
       compress = "bzip2")
}

这行得通吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多