[R] Loop

Matevž Pavlič matevz.pavlic at gi-zrmk.si
Thu Nov 4 14:21:38 CET 2010


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?


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



More information about the R-help mailing list