In R:How do I itearate through character strings in a loop? -


i'm trying access character strings vector in for-loop.

i have corpus one:

library(tm) corpus = corpus(vectorsource(c("cfilm,cgame,ccd","cd,film,cfilm"))) 

my goal rid off unnecessary "c" characters. note, means don't want remove c cd, ccd, cgame , forth.

i use function, takes in corpus , removes term second one.

tostring = content_transformer(function(x,from,to)gsub(from, to, x)) 

so, example, replace cgame game, use

corpus = tm_map(corpus,tostring,"cgame","game") 

now, instead of repeating line terms, i'd use loop iterates possible replacements using vector relevant terms.

replace = c("game","film","cd") 

i tried 2 approaches, none of them work:

for(i in replace){tm_map(corpus,tostring,paste("c",get(i),sep=""),get(i))} 

and

for(i in 1:length(replace)){tm_map(corpus,tostring,paste("c",replace[i],sep=""),replace[i])} 

in first case r tells me can't find object received get(i): error in get(i) : object 'game' not found. in second, there no error message, nothing changes within corpus.

how can query items within vector strings, for-loop repeat terms, did corpus = tm_map(corpus,tostring,"cgame","game)

the tm_map function doesn't modify corpus in place, returns modified corpus. right not doing save result. try

 for(i in 1:length(replace)){      corpus <- tm_map(corpus,tostring,paste("c",replace[i],sep=""),replace[i])   } 

also, tostring name of function in base r it's not idea write own function same name.

finally, get() method returns value of r variable same name character value pass it. there no reason use get() here since want continue working strings , not variable names.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -