[R] Transforming a string to a variable's name? help me newbie...

Greg Snow Greg.Snow at imail.org
Mon Dec 8 22:10:38 CET 2008


I really don't understand your concern.  Something like:

> nms <- c('file1','file2','file3')
> my.data <- list()
> for (i in nms) my.data[[ i ]] <- read.table(i)

Will read in the files listed in the nms vector and put them into the list my.data (each data frame is a single element of the list).  This list will not take up about the same amount of memory as if you read each file into a dataframe in the global environment.  And there is no transforming of data frames (into 1 row or otherwise).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: tsunhin wong [mailto:thjwong at gmail.com]
> Sent: Monday, December 08, 2008 1:34 PM
> To: Greg Snow
> Cc: Jim Holtman; r-help at r-project.org; jorgeivanvelez at gmail.com
> Subject: Re: [R] Transforming a string to a variable's name? help me
> newbie...
>
> I want to combine all dataframes into one large list too...
> But each dataframe is a 35 columns x varying number of rows structure
> (from 2000 to >9000 rows)
> I have ~1500 dataframes of these to process, and that add up to >
> 1.5Gb of data...
>
> Combining dataframes into a single one require me to transform each
> single dataframe into one line, but I really don't have a good
> solution for the varying number of rows scenario... And also, I don't
> want to stall my laptop every time I run the data set: maybe I can do
> that when my prof give me a ~ 4Gb ram desktop to run the script ;)
>
> Thanks! :)
>
> - John
>
> On Mon, Dec 8, 2008 at 1:36 PM, Greg Snow <Greg.Snow at imail.org> wrote:
> > In the long run it will probably make your life much easier to read
> all the dataframes into one large list (and have the names of the
> elements be what your currently name the dataframes), then you can just
> use regular list indexing (using [[]] rather than $ in most cases)
> instead of having to worry about get and assign and the
> risks/subtleties involved in using those.
> >
> > Hope this helps,
> >
> > --
> > Gregory (Greg) L. Snow Ph.D.
> > Statistical Data Center
> > Intermountain Healthcare
> > greg.snow at imail.org
> > 801.408.8111
> >
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> >> project.org] On Behalf Of tsunhin wong
> >> Sent: Monday, December 08, 2008 8:45 AM
> >> To: Jim Holtman
> >> Cc: r-help at r-project.org
> >> Subject: Re: [R] Transforming a string to a variable's name? help me
> >> newbie...
> >>
> >> Thanks Jim and All!
> >>
> >> It works:
> >> tmptrial <- trialcompute(trialextract(
> >> get(paste("t",tmptrialinfo[1,2],tmptrialinfo[1,16],".gz",sep="")) ,
> >> tmptrialinfo[1,32],secs,sdm),secs,binsize)
> >>
> >> Can I use "assign" instead? How should it be coded then?
> >>
> >> Thanks!
> >>
> >> - John
> >>
> >> On Mon, Dec 8, 2008 at 10:40 AM, Jim Holtman <jholtman at gmail.com>
> >> wrote:
> >> > ?get
> >> >
> >> >
> >> > Sent from my iPhone
> >> >
> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <thjwong at gmail.com> wrote:
> >> >
> >> >> Dear all,
> >> >>
> >> >> I'm a newbie in R.
> >> >> I have a 45x2x2x8 design.
> >> >> A dataframe stores the metadata of trials. And each trial has its
> >> own
> >> >> data file: I used "read.table" to import every trial into R as a
> >> >> dataframe (variable).
> >> >>
> >> >> Now I dynamically ask R to retrieve trials that fit certain
> >> selection
> >> >> criteria, so I use "subset", e.g.
> >> >> tmptrialinfo <- subset(trialinfo, (Subject==24 &
> >> Filename=="v2msa8"))
> >> >>
> >> >> The name of the dataframe / variable of an individual trial can
> be
> >> >> obtained using:
> >> >> paste("t",tmptrialinfo[1,2],tmptrialinfo[1,16],".gz",sep="")
> >> >> Then I get a string:
> >> >> "t24v2msa8.gz"
> >> >> which is of the exact same name of the dataframe / variable of
> that
> >> >> trial, which is:
> >> >> t24v2msa8.gz
> >> >>
> >> >> Can somebody tell me how can I change that string (obtained from
> >> >> "paste()" above) to be a usable / manipulable variable name, so
> that
> >> I
> >> >> can do something, such as:
> >> >> (1)
> >> >> tmptrial <- trialcompute(trialextract(
> >> >> paste("t",tmptrialinfo[1,2],tmptrialinfo[1,16],".gz",sep="")
> >> >> ,tmptrialinfo[1,32],secs,sdm),secs,binsize)
> >> >> instead of hardcoding:
> >> >> (2)
> >> >> tmptrial <-
> >> >>
> >>
> trialcompute(trialextract(t24v2msa8.gz,tmptrialinfo[1,32],secs,sdm),sec
> >> s,binsize)
> >> >>
> >> >> Currently, 1) doesn't work...
> >> >>
> >> >> Thanks in advance for your help!
> >> >>
> >> >> Regards,
> >> >>
> >> >>     John
> >> >>
> >> >> ______________________________________________
> >> >> 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