Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用sub或gsub_R_Gsub - Fatal编程技术网

在R中使用sub或gsub

在R中使用sub或gsub,r,gsub,R,Gsub,我有一个文件,里面有按年级和科目列出的问题和答案。我 需要在问题中插入路径。路径由以下部分组成: 固定URL、主题、等级和图像名称(问题中有)。我有 我们已经能够构建路径,但不知道如何使用sub 用新值替换旧值。以下是我到目前为止的情况: x<-read.csv( 'E:/My Documents/RED-985_second_try_2013022802.csv' , header = TRUE, sep = ",", fill = TRUE , quote="\"", na.str

我有一个文件,里面有按年级和科目列出的问题和答案。我 需要在问题中插入路径。路径由以下部分组成: 固定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 = ""))
我做错了什么,或者我应该如何看待它?非常感谢你
提前。

接近尾声时,您有:

x[[i],3]
这个逗号是出乎意料的。查看
x
的早期用法,您可能需要:

x[target.rows[i],3]
x[target.rows[i],3]