保存在R中的函数内

时间:2015-01-27 17:34:51

标签: r save

我在R中创建了这个函数,但是应该保存在目录中的输出不是预期的。 当我打印i变量时,它总是值1,所以我保存的文件的名称总是我在矢量的第一个位置上的名称。

function_filtering <- function(x){

upname <- c("up_toptable_alk.Rda", "up_toptable_kcbc1.Rda",   "up_toptable_kwt.Rda", "up_toptable_NaHog1.Rda", "up_toptable_Nawt.Rda","up_toptable_ox.Rda", "up_toptable_ter.Rda", "up_toptable_ypd.Rda")
downname <- c("down_toptable_alk.Rda", "down_toptable_kcbc1.Rda", "down_toptable_kwt.Rda", "down_toptable_NaHog1.Rda", "down_toptable_Nawt.Rda", "down_toptable_ox.Rda", "down_toptable_ter.Rda", "down_toptable_ypd.Rda")

p.value.cut <- which(x$P.Value < 0.05)
x <- x[p.value.cut,]    

up <- which(x$t > 0)
down <- which(x$t < 0)

up.p.value <- x[up,]
down.p.value <- x[down,]


save(up.p.value, file = upname[i])
save(down.p.value, file = downname[i])


i <- i + 1
print(i)

}

lapply(data, function_filtering)

感谢。

1 个答案:

答案 0 :(得分:0)

如果没有可重复的示例,我无法解决您的确切问题。但我认为这就是你的目标。

data <- c('a', 'b', 'c', 'd') 
toy <- function(x, d) { 
    # x is the integer from seq_along
    # d is the data you want to subset
    paste(x, d[x], sep='_')
}
# Then use lapply to run the function on each element of data
# extra arguments can be passed in at the end 
lapply(seq_along(data), toy, d=data)

输出

[[1]]
[1] "1_a"

[[2]]
[1] "2_b"

[[3]]
[1] "3_c"

[[4]]
[1] "4_d"