[R] Loop

Petr PIKAL petr.pikal at precheza.cz
Fri Nov 5 06:48:36 CET 2010


Hi

the list/loop solution given below enables to give you both.  You can 
transform it to data,frame

data.frame(do.call(cbind, lll),sapply(lll, names))

and add appropriate names

or you can use Jim's solution and combine those 2 steps

Regards
Petr


Matevž Pavlič <matevz.pavlic at gi-zrmk.si> napsal dne 05.11.2010 00:05:00:

> Hi Jim, 
> 
> Actually, this is better, but both values are what i am looking for. 
Count and
> the value of the count. 
> Is there a way to just paste those two together?
> 
> Thanks, m
> 

<snip>

> >
> > 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.
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?



More information about the R-help mailing list