带有for循环的R脚本中的语法错误

时间:2018-02-09 10:42:22

标签: r

我正在编写一个简单的R脚本(script.r)。

我希望R读入13个连续编号的表(" table1.txt"," table2.txt"等),然后选择一个特定的列("剂量"),并将13个输出中的每一个乘以不同的因子,然后将它们全部加在一起。

for (i in 1:13) {
    loci[i]<-read.table("table[i].txt, header=T)
    dloc[i]<-(loc[i]$dosage)
    logdloc1<-(dloc1*0.077)
    logdloc2<-(dloc2*-0.105)
    logdloc3<-(dloc3*0.104)
    logdloc4<-(dloc4*0.113)
    logdloc5<-(dloc5*-0.105)
    logdloc6<-(dloc6*0.131)
    logdloc7<-(dloc7*-0.117)
    logdloc8<-(dloc8*-0.083)
    logdloc9<-(dloc9*-0.083)
    logdloc10<-(dloc10*0.182)
    logdloc11<-(dloc11*0.157)
    logdloc12<-(dloc12*0.086)
    logdloc13<-(dloc13*0.27)
    output<-(logdloc1 + logdloc2 + logdloc3 + logdloc4 + logdloc5 + logdloc6 + logdloc7 + logdloc8 + logdloc9 + logdloc10 + logdloc11 + logdloc12 + logdloc13)}

我知道语法错误,因为当我尝试在命令行中执行它时收到错误消息:

Rscript script.r
Error: unexpected end of input

有人可以非常友善地发现这一点吗?

非常感谢你的帮助。

AB

1 个答案:

答案 0 :(得分:2)

你遗失了&#34;

for (i in 1:13) {
    loci[i]<-read.table("table[i].txt", header=T)
    dloc[i]<-(loc[i]$dosage)
    logdloc1<-(dloc1*0.077)
    logdloc2<-(dloc2*-0.105)
    logdloc3<-(dloc3*0.104)
    logdloc4<-(dloc4*0.113)
    logdloc5<-(dloc5*-0.105)
    logdloc6<-(dloc6*0.131)
    logdloc7<-(dloc7*-0.117)
    logdloc8<-(dloc8*-0.083)
    logdloc9<-(dloc9*-0.083)
    logdloc10<-(dloc10*0.182)
    logdloc11<-(dloc11*0.157)
    logdloc12<-(dloc12*0.086)
    logdloc13<-(dloc13*0.27)
    output<-(logdloc1 + logdloc2 + logdloc3 + logdloc4 + logdloc5 + logdloc6 + logdloc7 + logdloc8 + logdloc9 + logdloc10 + logdloc11 + logdloc12 + logdloc13)}

修改

基于coment,我猜table是一个r对象,所以你应该用以下代码替换:

loci[i]<-read.table(paste0(table[i], ".txt"), header=T)

将第i个元素粘贴到&#34; .txt&#34;。

如需更完整的答案,请详细说明table

中的内容