[R] Loop

Petr PIKAL petr.pikal at precheza.cz
Thu Nov 4 16:55:31 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 04.11.2010 15:49:31:

> Hi all, 
> 
> I understand that you most of you this is a peice of cake but i am a 
complete 
> newbie in this....so any example would be greatly aprpeciated and also 
any 
> hint as how to get around in R. Frankly i sometimes see the help files 
kinda confusing.

OK. Instead of 
> > >> 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)

Suppose you have data frame or matrix, and you want to have 5 most common 
values from each column

# prepare matrix
x<-sample(1:20, 1000000, replace=T)
mat<-matrix(x, ncol=10)

#apply user defined function for each column
apply(mat, 2, function(x) head(sort(table(x), decreasing=T),5))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 5091 5135 5174 5133 5199 5097 5165 5157 5134  5068
[2,] 5073 5111 5143 5064 5113 5078 5102 5157 5131  5065
[3,] 5058 5092 5115 5051 5079 5064 5088 5128 5076  5063
[4,] 5056 5073 5114 5047 5059 5044 5037 5064 5071  5063
[5,] 5047 5064 5072 5041 5057 5041 5035 5058 5032  5061

If you want to do it in loop (can be quicker sometimes) and save it to 
list make a list

lll<-vector("list", 10)

and fill it with your results

for (i in 1:10) lll[[i]]<-head(sort(table(mat[,i]), decreasing=T),5)

and now you can call values from this lll list simply by

lll[5]
[[1]]

   9   15   13    6   16 
5199 5113 5079 5059 5057 

lll[[5]]

   9   15   13    6   16 
5199 5113 5079 5059 5057

or even

lll[[5]][3]
  13 
5079

without need for writing to individual files pasting together letters and 
numbers etc. 

There shall be R-intro document in your installation and it is worth 
reading. It is not so big, you can manage it in less then month if you 
complete more than 3 pages per day.

Regards
Petr



> 
> M
> 
> -----Original Message-----
> From: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
> Sent: Thursday, November 04, 2010 3:40 PM
> To: Matevž Pavlič
> Cc: r-help at r-project.org
> Subject: Re: [R] Loop
> 
> Hi
> 
> r-help-bounces at r-project.org napsal dne 04.11.2010 14:21:38:
> 
> > Hi David,
> > 
> > I am still having troubles with that loop ...
> > 
> > This code gives me (kinda) the name of the column/field in a data 
frame. 
> Filed
> > names are form W1-W10. But there is a space between W and a number -->
> "W 10",
> > and column (field) names do not contain numbers. 
> > 
> > >for(i in 1:10)
> > >{
> > >vari <- paste("W",i)
> > >}
> > >vari
> > 
> > [1] "W 10"
> > 
> > Now as i understand than i would call different columns to R with
> > 
> > >w<-lit[[vari]]
> > 
> > Or am i wrong again?
> > 
> > Then I would probably need another loop to create the names of the
> variables 
> > on R, i.e. w1 to w10. Is that a general idea for the procedure?
> 
> Beware of such loops. Instead of littering your workspace with 
files/objects 
> constructed by some paste(whatever, i) solution you can save results in 
list 
> or data.frame or matrix and simply use basic subsetting procedures or 
lapply/
> sapply functions.
> 
> I must say I never used such paste(...) construction yet and I work with 
R for
> quite a long time.
> 
> Regards
> Petr
> 
> 
> > 
> > 
> > Thank for the help, m
> > 
> > -----Original Message-----
> > From: David Winsemius [mailto:dwinsemius at comcast.net]
> > Sent: Wednesday, November 03, 2010 10:41 PM
> > To: Matevž Pavlič
> > Cc: r-help at r-project.org
> > Subject: Re: [R] Loop
> > 
> > 
> > On Nov 3, 2010, at 5:03 PM, Matevž Pavlič wrote:
> > 
> > > 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)
> > 
> > It appears you are not reading for meaning. Burns has advised you how 
> > to
> 
> > construct column names and use them in your initial steps. The `$`
> function is
> > quite limited in comparison to `[[` , so he was showing you a method
> that 
> > would be more effective.  BTW the as.data.frame step is unnecessary,
> since the
> > first thing write.table does is coerce an object to a data.frame. The 
> > "write.table" name is misleading. It should be "write.data.frame". You
> cannot 
> > really write tables with write.table.
> > 
> > You would also use:
> > 
> >   file=paste(vari, "csv", sep=".") as the file argument to write.table
> > 
> > > write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
> > 
> > What are these next actions supposed to do after the file is written? 
> > Are you trying to store a group of related "w" objects that will later
> be 
> > indexed in sequence? If so, then a list would make more sense.
> > 
> > --
> > David.
> > 
> > > 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]
> > > 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
> > 
> > >
> > 
> > David Winsemius, MD
> > West Hartford, CT
> > 
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list