2016-04-06 12 views
1

Ich habe versucht, Code wie unten auszuführen. Ich frage mich, warum die gsub-Funktion bei dieser Eingabe nicht funktioniert hat. Wer weiß, warum und wie mit diesem Fall umzugehen?R, gsub funktioniert nicht

> text 

[1] <a href="https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4" rel="nofollow">UberSocial for Twitter on iOS</a> 
65 Levels: <a href="http://aktualpost.com" rel="nofollow">Aktualpost</a> ... 
> start = as.numeric(regexpr(">",text)[[1]])+1 
> start 
[1] 103 
> to_cut = substr(text,1,start-1) 
> to_cut 
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">" 
> new_text = gsub(to_cut,"",as.character(text)) 
> new_text 
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">UberSocial for Twitter on iOS</a>" 
+0

'Text' ist ein Faktor. Zuerst konvertieren Sie es in das Zeichen: 'as.character (text)' – jogo

+1

Es ist, weil es '?', Die nicht mit dem 'text' übereinstimmt – akrun

+0

Dies ist kein guter Titel für eine Frage. –

Antwort

1

Es gibt ? in "to_cut", die nicht in "text" zu finden ist. Wenn wir das beheben, sollte es funktionieren, d.h. überprüfen Sie ?mt in "to_cut" und mt in "Text".

gsub("^<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">(.*)", "\\1", text) 
#[1] "UberSocial for Twitter on iOS</a>" 

Es ist nicht klar, wie die OP die "to_cut" bekam mit dem ?

start = as.numeric(regexpr(">",text)[[1]])+1 
to_cut <-substr(text,1,start-1) 
to_cut 
#[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">" 
gsub(to_cut, "", text) 
#[1] "UberSocial for Twitter on iOS</a>"