[R] substring and paste character with a for loop

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Thu Feb 3 17:48:07 CET 2011


Hi Chris,
'gsub' does exactly what you want:

gsub(",","",text)

so there is no need for a loop.

Btw. your loop assigns a new value to m in each step and you see only
the last assignment.
you could do something like

m <- paste(m,substring(text,i,i+2),sep = "")

hth

Am 03.02.2011 17:35, schrieb Chris82:
> 
> Hello R users,
> 
> I have a little problem with a for loop.
> Below there is an simple example of my problem.
> 
> I want to delet the commas in the character string. Fore this reason I
> create a for loop to unpick the string and rebuild him without the commas.
> The problem is, that "paste" does not work in the loop as I expected.
> 
> text <- "aaa,bbb,ccc,ddd"
> 
> characterseq <- seq(1,15,4)
> 
> for (i in characterseq ){
> m <- paste(substring(text,i,i+2),sep = "")
> }
> 
>> m
> [1] "ddd"
> 
> 
> m should be "aaabbbcccddd" and not just "ddd"
> 
> 
> 
> with best regards
> 
> Chris


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list