Loop over a split string

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop over a split string

Joel
Hi

I got a string that looks like this:

string<-"a b c d e f"

And what I wanna do is loop trough all the letters.
like

for(i in string){
 print(i)
}

would render the result:
a
b
c
d
e
f

Ive tried using strsplit but without result, dose anyone know how I could make this happen?

//Joel
Reply | Threaded
Open this post in threaded view
|

Re: Loop over a split string

Uwe Ligges-3


On 11.05.2011 11:12, Joel wrote:

> Hi
>
> I got a string that looks like this:
>
> string<-"a b c d e f"
>
> And what I wanna do is loop trough all the letters.
> like
>
> for(i in string){
>   print(i)
> }
>
> would render the result:
> a
> b
> c
> d
> e
> f
>
> Ive tried using strsplit but without result, dose anyone know how I could
> make this happen?

Please ask your supervisor for advices related to your homework.

Uwe Ligges


>
> //Joel
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-over-a-split-string-tp3514204p3514204.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over a split string

Joel
This is no homework, Im just trying to learn R but sorry for wasting your time you all mighty God of R Uwe Ligges.

And if this is not a forum to ask simple questions can you please redirect me to where I might get help?

//Joel
Reply | Threaded
Open this post in threaded view
|

Re: Loop over a split string

Paul Hiemstra-2
 Hi Joel,

This looks so much like homework I would give studens that I'm not
surprised Uwe jumped to conclusions...

ontopic:
You did not specify your earlier attempts, just that they failed. This
makes it hard for us to judge what went wrong. Using strsplit I would do:

string<-"a b c d e f"
# The important bit is the subset ([[1]])
# strsplit can work on a vector of strings
# so just splitting on string requires this step
splitted <- strsplit(string, ' ')[[1]]
for(entry in splitted) print(entry)

cheers,
Paul

On 05/11/2011 09:22 AM, Joel wrote:

> This is no homework, Im just trying to learn R but sorry for wasting your
> time you all mighty God of R Uwe Ligges.
>
> And if this is not a forum to ask simple questions can you please redirect
> me to where I might get help?
>
> //Joel
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-over-a-split-string-tp3514204p3514225.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


--
Paul Hiemstra, MSc
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over a split string

Joel
Thx

Paul

My string aint that simple its just that if it works for this simple example it will work for my string therefor I just used the "a b c d e f" syntax.
Reply | Threaded
Open this post in threaded view
|

Re: Loop over a split string

Peter Dalgaard-2

On May 11, 2011, at 11:35 , Joel wrote:

> Thx
>
> Paul
>
> My string aint that simple its just that if it works for this simple example
> it will work for my string therefor I just used the "a b c d e f" syntax.


> strsplit(string," ")
[[1]]
[1] "a" "b" "c" "d" "e" "f"

> strsplit(string," ")[[1]]
[1] "a" "b" "c" "d" "e" "f"
> for(i in strsplit(string," ")[[1]]) cat(i, "\n")
a
b
c
d
e
f
> cat(strsplit(string," ")[[1]], sep="\n")
a
b
c
d
e
f
>


--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.