[R] looping through data frames in a workspace

2008-03-03 Thread lucy b
All,

I have a workspace containing only data frame objects. I would like to
loop though each one and clean-up text columns in them. How can I have
R loop through the list? I have tried to find an answer in R help but
the closest solution I can find is to make a static list of data
frames, as illustrated in this recent post:

-begin post
On Tuesday 19 February 2008 (19:51:15), TLowe wrote:
> Hey Folks,
>
> Could somebody show me how to loop through a list of dataframes?  I want to
> be able to generically access their elements and do something with them.
>
> For instance, instead of this:
>
> df1<- data.frame(x=(1:5),y=(1:5));
> df2<- data.frame(x=(1:5),y=(1:5));
> df3<- data.frame(x=(1:5),y=(1:5));
> plot(df1$x,df1$y);
> plot(df2$x,df2$y);
> plot(df3$x,df3$y);
>
> I would like to do something like:
> (pseudocode)
> dfarray[1] = df1
> dfarray[2] = df2
> dfarray[3] = df3
> for (each i in dfarray) {
>   plot(i$x, i$y);
> }

It's surprisingly simple:

for(df in list(df1,df2,df3)) plot(df$x,df$y)

Best,

Martin
-end post

I would like to avoid having to type-out a very long list over and
over again. I have tried every variation I could think of similar to:

for(df in list(noquote(ls( {

 do stuff with df

 }

I know this has to be possible. What am I missing?

Many Thanks-

__
R-help@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] looping through data frames in a workspace

2008-03-04 Thread lucy b
Thanks for helpful replies. Unfortunately, I didn't mention that I
want to overwrite the existing data frame with the corrected one using
the same name. Here's what I have:

## get names of data frames
frames <- names(Filter(function(x) x=="data.frame",
   sapply(objects(),function(x)
  class(eval(parse(text=x))
## loop through list
for( i in seq(along=frames) ) {

#don't need next line, but good to remember if need to exclude a df
#if (frames[i] != "junk") {

df <- eval(parse(text=frames[i]))

for (c in 1:ncol(df)){
if (is.factor(df[,c])) {
df[,c] <- gsub("\r\n","",as.vector(df[,c]))
}
}

names(df) <- frames[i]

}#end first if

}#end first for
#END

Unfortunately, the "names(df)<-frames[i]" line just changes the name
of the first column in df. How can I overwrite the existing data frame
using the right name?

__
R-help@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] extracting data using strings as delimiters

2007-09-25 Thread lucy b
Dear List,

I have an ascii text file with data I'd like to extract. Example:

Year Built:  1873 Gross Building Area:  578 sq ft
Total Rooms:  6 Living Area:  578 sq ft

There is a lot of data I'd like to ignore in each record, so I'm
hoping there is a way to use strings as delimiters to get the data I
want (e.g. tell R to take data between "Built:" and "Gross" -
incidentally, not always numeric). I think an ugly way would be to
start at the end of each record and use a substitution expression to
chip away at it, but I'm afraid it will take forever to run. Is there
a way to use strings as delimiters in an expression?

Thanks in advance for ideas.

LB

__
R-help@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.


Re: [R] extracting data using strings as delimiters

2007-09-26 Thread lucy b
All great ideas. I tried strsplit first and it worked, but thanks everyone!

Best-
LB

On 9/25/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
> Perhaps you could clarify what the general rule is but assuming
> that what you want is any word after a colon it can be done with
> strapply in the gsubfn package like this:
>
> Lines <- c("Year Built:  1873 Gross Building Area:  578 sq ft",
> "Total Rooms:  6 Living Area:  578 sq ft")
>
> library(gsubfn)
> strapply(Lines, ": *(\\w+)", backref = -1)
>
> # or if each line has same number of returned words
> strapply(Lines, ": *(\\w+)", backref = -1, simplify = rbind)
>
> This matches a colon (:) followed by zero or more spaces ( *)
> followed by a word ((\\w+)) and backref= - 1 causes it to return
> only the first backreference (i..e. the portion within parentheses)
> but not the match itself.
>
> On 9/25/07, lucy b <[EMAIL PROTECTED]> wrote:
> > Dear List,
> >
> > I have an ascii text file with data I'd like to extract. Example:
> >
> > Year Built:  1873 Gross Building Area:  578 sq ft
> > Total Rooms:  6 Living Area:  578 sq ft
> >
> > There is a lot of data I'd like to ignore in each record, so I'm
> > hoping there is a way to use strings as delimiters to get the data I
> > want (e.g. tell R to take data between "Built:" and "Gross" -
> > incidentally, not always numeric). I think an ugly way would be to
> > start at the end of each record and use a substitution expression to
> > chip away at it, but I'm afraid it will take forever to run. Is there
> > a way to use strings as delimiters in an expression?
> >
> > Thanks in advance for ideas.
> >
> > LB
> >
> > __
> > R-help@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@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.