[R] Loop

Matevž Pavlič matevz.pavlic at gi-zrmk.si
Wed Nov 3 22:03:53 CET 2010


Hi, 

Thanks for the help and the manuals. Will come very handy i am sure. 

But regarding the code i don't hink this is what i want....basically i would like to repeat bellow code :

w1<-table(lit$W1)
w1<-as.data.frame(w1)
write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
w1<- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20)

20 times, where W1-20 (capital letters) are the fields in a data.frame called "lit" and w1-20 are the data.frames being created.

Hope that explains it better, 

m


-----Original Message-----
From: Patrick Burns [mailto:pburns at pburns.seanet.com]
Sent: Wednesday, November 03, 2010 9:30 PM
To: Matevž Pavlič
Subject: Re: [R] Loop

If I understand properly, you'll want
something like:

lit[["w2"]]

instead of

lit$w2

more accurately:

for(i in 1:20) {
vari <- paste("w", i)
lit[[vari]]

...
}

The two documents mentioned in my
signature may help you.

On 03/11/2010 20:23, Matevž Pavlič wrote:
> Hi all,
>
>
>
> I managed to do what i want (with the great help of thi mailing list)  manually . Now i would like to automate it. I would probably need a for loop for to help me with this...but of course  I have no idea how to do that in R.  Bellow is the code that i would like to be replicated for a number of times (let say 20). I would like to achieve  that w1 would change to w2, w3, w4 ... up to w20 and by that create 20 data.frames that I would than bind together with cbind.
>
>
>
> (i did it like shown bellow -manually)
>
>
>
> w1<-table(lit$W1)
>
> w1<-as.data.frame(w1)
>
> write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
>
> w1<- w1[order(w1$Freq, decreasing=TRUE),]
>
> w1<-head(w1, 20)
>
>
>
>
>
> w2<-table(lit$W2)
>
> w2<-as.data.frame(w2)
>
> write.table(w2,file="w2.csv",sep=";",row.names=T, dec=".")
>
> w2<- w2[order(w2$Freq, decreasing=TRUE),]
>
> w2<-head(w2, 20)
>
>
>
> .
>
> .
>
> .
>
> Thanks for the help,m
>
>
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>

--
Patrick Burns
pburns at pburns.seanet.com
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')


More information about the R-help mailing list