0

我有一个文件,其中包含按年级和科目划分的问题和答案。我需要将路径插入问题中。路径由固定的 URL、主题、等级和图像名称(在问题中)组成。我已经能够构建路径,但无法弄清楚如何使用 sub 将旧值替换为新值。这是我到目前为止所拥有的:

 x<-read.csv( 'E:/My Documents/RED-985_second_try_2013022802.csv' , header =
 TRUE, sep = ",", fill = TRUE , quote="\"", na.strings="NA")

 ## Here we determine which rows contain a line that begins with 'img src',
 since those are the ones we want.
 target.rows <- grep(x[,3], pattern = "img src=.*\\.gif")
 new.string <- ""
 grade.string<-""
 subject.string<-""

 #Will need to look through each column by changing the value in the
 target.row command so that I bet all images

 for (i in 1: length(target.rows))
 {
    subject<-x[target.rows[i],12]
    grade<-x[target.rows[i],2]
    string.to.check <- x[target.rows[i],3]
    first.quote.pos <- gregexpr(pattern='"', string.to.check)[[1]][1] + 1
    second.quote.pos <- gregexpr(pattern='"', string.to.check)[[1]][2] - 1
    new.string[i] <- substring(string.to.check,first.quote.pos,second.quote.pos)                                                        

    if (nchar(grade) == 1)
    {
            grade<-paste(0,grade,sep="")
    }

    if (subject == 1)
    {
            subject<-"MA"
    }
    else
    {
        subject<-"RE"
    }

    grade.string[i] <-grade

    subject.string[i]<-subject

    url.string2<-sub(, paste("http://ABC.com/_practice/",
    subject,"-",grade,"/",new.string[i], sep = ""), x[[i],3])
    gsub("img src=.*\\.gif", "url.string2", x)


     }
     warnings(url.strings <- paste("http://ABC.com/_practice/",
     subject.string,"-",grade.string,"/",new.string, sep = ""))

我收到这些错误消息:

Running E:\My Documents\Revolution\Project_One\Project_One\GSUB_Try.R ...
E:\My Documents\Revolution\Project_One\Project_One\GSUB_Try.R(38): Error:
unexpected ','

我做错了什么或者我应该如何看待它?非常感谢您提前。

4

1 回答 1

1

接近尾声,你有这个:

x[[i],3]

那个逗号是出乎意料的。查看 的早期用法x,您可能想要这样:

x[target.rows[i],3]
于 2013-03-10T22:35:45.603 回答