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

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

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

tsunhin wong
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),secs,binsize)

Currently, 1) doesn't work...

Thanks in advance for your help!

Regards,

      John

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

David Winsemius

On Dec 8, 2008, at 10:11 AM, tsunhin wong 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),secs,binsize)
>

It may have something to do with the semantics of trialextract about  
which most useRs will know nothing (and I was unsuccessful in finding  
a function by that name with an Rsitesearch). It seems strange that a  
function would accept an unquoted reference to what is obviously a  
file name. Perhaps you should be offering that code for inspection  
rather than asking how to make a simple assignment. It is possible  
that what you are really asking is answered by Faq 7.21:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f

> Currently, 1) doesn't work...

Saying "doesn't work" is not specific enough. What exactly was the  
error message?

>
>
> Thanks in advance for your help!
>
> Regards,
>
>      John
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

jholtman
In reply to this post by tsunhin wong
?get


Sent from my iPhone

On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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),secs,binsize)
>
> Currently, 1) doesn't work...
>
> Thanks in advance for your help!
>
> Regards,
>
>      John
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

Jorge I Velez
In reply to this post by tsunhin wong
Dear Tsunhin,
Take a look at ?get

HTH,

Jorge


On Mon, Dec 8, 2008 at 10:11 AM, tsunhin wong <[hidden email]> 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),secs,binsize)
>
> Currently, 1) doesn't work...
>
> Thanks in advance for your help!
>
> Regards,
>
>      John
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

tsunhin wong
In reply to this post by jholtman
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 <[hidden email]> wrote:

> ?get
>
>
> Sent from my iPhone
>
> On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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),secs,binsize)
>>
>> Currently, 1) doesn't work...
>>
>> Thanks in advance for your help!
>>
>> Regards,
>>
>>     John
>>
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

Greg Snow-2
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of tsunhin wong
> Sent: Monday, December 08, 2008 8:45 AM
> To: Jim Holtman
> Cc: [hidden email]
> 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 <[hidden email]>
> wrote:
> > ?get
> >
> >
> > Sent from my iPhone
> >
> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

tsunhin wong
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 <[hidden email]> 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
> [hidden email]
> 801.408.8111
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of tsunhin wong
>> Sent: Monday, December 08, 2008 8:45 AM
>> To: Jim Holtman
>> Cc: [hidden email]
>> 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 <[hidden email]>
>> wrote:
>> > ?get
>> >
>> >
>> > Sent from my iPhone
>> >
>> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >
>>
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

Greg Snow-2
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: tsunhin wong [mailto:[hidden email]]
> Sent: Monday, December 08, 2008 1:34 PM
> To: Greg Snow
> Cc: Jim Holtman; [hidden email]; [hidden email]
> 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 <[hidden email]> 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
> > [hidden email]
> > 801.408.8111
> >
> >
> >> -----Original Message-----
> >> From: [hidden email] [mailto:r-help-bounces@r-
> >> project.org] On Behalf Of tsunhin wong
> >> Sent: Monday, December 08, 2008 8:45 AM
> >> To: Jim Holtman
> >> Cc: [hidden email]
> >> 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 <[hidden email]>
> >> wrote:
> >> > ?get
> >> >
> >> >
> >> > Sent from my iPhone
> >> >
> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
> >> >>
> >> >> ______________________________________________
> >> >> [hidden email] 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.
> >> >
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

tsunhin wong
Greg,

I found your suggestion very efficient! And I tried testing it,
however, it didn't work:
###start of file
rm(list=ls())

setwd("/Users/John/Documents/Research/Experiments/expt108/")

##test out 3 files only
nms <- c("019-v1-msa1.data","019-v1-msa2.data","019-v1-msa3.data")

my.data <- list()

for(i in nms) my.data[[i]] <- read.table(i)

setwd("/Users/John/Programs/R/")
save(list=ls(all=TRUE), file="Expt108Files.Rdata")
###end of file

At first, I get this error:
Expt108Files.R: unexpected symbol at
10: ,"019--v1-msa1.gazedata"

Then I renames the 3 files to the followings:
nms <- c("019v1msa1.data","019v1msa2.data","019v1msa3.data")

And I received this error instead:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  line 2 did not have 36 elements

Thanks!

- John

On Mon, Dec 8, 2008 at 4:10 PM, Greg Snow <[hidden email]> wrote:

> 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
> [hidden email]
> 801.408.8111
>
>
>> -----Original Message-----
>> From: tsunhin wong [mailto:[hidden email]]
>> Sent: Monday, December 08, 2008 1:34 PM
>> To: Greg Snow
>> Cc: Jim Holtman; [hidden email]; [hidden email]
>> 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 <[hidden email]> 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
>> > [hidden email]
>> > 801.408.8111
>> >
>> >
>> >> -----Original Message-----
>> >> From: [hidden email] [mailto:r-help-bounces@r-
>> >> project.org] On Behalf Of tsunhin wong
>> >> Sent: Monday, December 08, 2008 8:45 AM
>> >> To: Jim Holtman
>> >> Cc: [hidden email]
>> >> 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 <[hidden email]>
>> >> wrote:
>> >> > ?get
>> >> >
>> >> >
>> >> > Sent from my iPhone
>> >> >
>> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
>> >> >>
>> >> >> ______________________________________________
>> >> >> [hidden email] 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.
>> >> >
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

tsunhin wong
In reply to this post by Greg Snow-2
I did really want to find make the suggestion work... it is going to
save my time...
I worked on trying to put all dataframes into a list() by:
###start of file
rm(list=ls())
setwd("/Users/John/Programs/R")
load("Expt108Master2008.Rdata")

nms <- ls(pattern=".data")
my.data <- list()
for(i in nms) my.data[[i]] <- get(i)

save(list=ls(pattern="my."), file="Expt108One.Rdata")
###end of file

The original Rdata file (with global dataframes) is 427.4Mb, and the
new Rdata file with 1 list() storing all dataframes is 427.2Mb
I can now access the individual dataframes by:
my.data$019v1msa1.data

But...
At the moment, whenever I type
>my.data
then the whole huge list of dataframes just flush out of standard
output... and doing a
>dim(my.data)
will give me only NULL...
what command(s) allows me to look at what variables / dataframes are
stored in a list() structure? And how many of them is there?

Thanks a lot! I'm still seasoning my style of working in R!

Regards,

      John

On Mon, Dec 8, 2008 at 4:10 PM, Greg Snow <[hidden email]> wrote:

> 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
> [hidden email]
> 801.408.8111
>
>
>> -----Original Message-----
>> From: tsunhin wong [mailto:[hidden email]]
>> Sent: Monday, December 08, 2008 1:34 PM
>> To: Greg Snow
>> Cc: Jim Holtman; [hidden email]; [hidden email]
>> 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 <[hidden email]> 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
>> > [hidden email]
>> > 801.408.8111
>> >
>> >
>> >> -----Original Message-----
>> >> From: [hidden email] [mailto:r-help-bounces@r-
>> >> project.org] On Behalf Of tsunhin wong
>> >> Sent: Monday, December 08, 2008 8:45 AM
>> >> To: Jim Holtman
>> >> Cc: [hidden email]
>> >> 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 <[hidden email]>
>> >> wrote:
>> >> > ?get
>> >> >
>> >> >
>> >> > Sent from my iPhone
>> >> >
>> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
>> >> >>
>> >> >> ______________________________________________
>> >> >> [hidden email] 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.
>> >> >
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

Antonio, Fabio Di Narzo
> names(my.data)
gives you the vector of 'names' of the list, which should be a good,
compact overview of the list contents.
> str(my.data)
gives also streamlined information about each component of the list,
i.e. also what variables are there (names and types), how many
observations, etc.
To dissect each single data.frame, just estract it and use it as a
regular data.frame, i.e.
> 019data <- my.data[["019v1msa1.data"]]
> summary(019data)
> 019data$var1
> plot(var1 ~ var2, data = 019data)
> lm(var1 ~ ., data = 019data)
etc. etc.

HTH,
antonio.

2008/12/9 tsunhin wong <[hidden email]>:

> I did really want to find make the suggestion work... it is going to
> save my time...
> I worked on trying to put all dataframes into a list() by:
> ###start of file
> rm(list=ls())
> setwd("/Users/John/Programs/R")
> load("Expt108Master2008.Rdata")
>
> nms <- ls(pattern=".data")
> my.data <- list()
> for(i in nms) my.data[[i]] <- get(i)

I don't get this bit. This really should be:
> for(i in nms) my.data[[i]] <- read.table(i)
as Greg explained.

>
> save(list=ls(pattern="my."), file="Expt108One.Rdata")
if you have done the above correctly, all your data is in the 'my.data' list:
save(my.data, file="allData.Rdata")

> ###end of file
>
> The original Rdata file (with global dataframes) is 427.4Mb, and the
> new Rdata file with 1 list() storing all dataframes is 427.2Mb
> I can now access the individual dataframes by:
> my.data$019v1msa1.data
>
> But...
> At the moment, whenever I type
>>my.data
> then the whole huge list of dataframes just flush out of standard
> output... and doing a
>>dim(my.data)
> will give me only NULL...
> what command(s) allows me to look at what variables / dataframes are
> stored in a list() structure? And how many of them is there?
>
> Thanks a lot! I'm still seasoning my style of working in R!
>
> Regards,
>
>      John
>
> On Mon, Dec 8, 2008 at 4:10 PM, Greg Snow <[hidden email]> wrote:
>> 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
>> [hidden email]
>> 801.408.8111
>>
>>
>>> -----Original Message-----
>>> From: tsunhin wong [mailto:[hidden email]]
>>> Sent: Monday, December 08, 2008 1:34 PM
>>> To: Greg Snow
>>> Cc: Jim Holtman; [hidden email]; [hidden email]
>>> 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 <[hidden email]> 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
>>> > [hidden email]
>>> > 801.408.8111
>>> >
>>> >
>>> >> -----Original Message-----
>>> >> From: [hidden email] [mailto:r-help-bounces@r-
>>> >> project.org] On Behalf Of tsunhin wong
>>> >> Sent: Monday, December 08, 2008 8:45 AM
>>> >> To: Jim Holtman
>>> >> Cc: [hidden email]
>>> >> 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 <[hidden email]>
>>> >> wrote:
>>> >> > ?get
>>> >> >
>>> >> >
>>> >> > Sent from my iPhone
>>> >> >
>>> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
>>> >> >>
>>> >> >> ______________________________________________
>>> >> >> [hidden email] 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.
>>> >> >
>>> >>
>>> >> ______________________________________________
>>> >> [hidden email] 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.
>>> >
>>
>
> ______________________________________________
> [hidden email] 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.
>



--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

tsunhin wong
Thank you all! :)

Can I "walk through" the my.data structure without knowing the name of
particular dataframe in it? Such as:
for(i in 1:length(my.data)) myprocess(my.data[i])
?
If it is possible, then this is going to save the work of using paste().

Thanks!

On Tue, Dec 9, 2008 at 6:45 PM, Antonio, Fabio Di Narzo
<[hidden email]> wrote:

>> names(my.data)
> gives you the vector of 'names' of the list, which should be a good,
> compact overview of the list contents.
>> str(my.data)
> gives also streamlined information about each component of the list,
> i.e. also what variables are there (names and types), how many
> observations, etc.
> To dissect each single data.frame, just estract it and use it as a
> regular data.frame, i.e.
>> 019data <- my.data[["019v1msa1.data"]]
>> summary(019data)
>> 019data$var1
>> plot(var1 ~ var2, data = 019data)
>> lm(var1 ~ ., data = 019data)
> etc. etc.
>
> HTH,
> antonio.
>
> 2008/12/9 tsunhin wong <[hidden email]>:
>> I did really want to find make the suggestion work... it is going to
>> save my time...
>> I worked on trying to put all dataframes into a list() by:
>> ###start of file
>> rm(list=ls())
>> setwd("/Users/John/Programs/R")
>> load("Expt108Master2008.Rdata")
>>
>> nms <- ls(pattern=".data")
>> my.data <- list()
>> for(i in nms) my.data[[i]] <- get(i)
>
> I don't get this bit. This really should be:
>> for(i in nms) my.data[[i]] <- read.table(i)
> as Greg explained.
>
>>
>> save(list=ls(pattern="my."), file="Expt108One.Rdata")
> if you have done the above correctly, all your data is in the 'my.data' list:
> save(my.data, file="allData.Rdata")
>
>> ###end of file
>>
>> The original Rdata file (with global dataframes) is 427.4Mb, and the
>> new Rdata file with 1 list() storing all dataframes is 427.2Mb
>> I can now access the individual dataframes by:
>> my.data$019v1msa1.data
>>
>> But...
>> At the moment, whenever I type
>>>my.data
>> then the whole huge list of dataframes just flush out of standard
>> output... and doing a
>>>dim(my.data)
>> will give me only NULL...
>> what command(s) allows me to look at what variables / dataframes are
>> stored in a list() structure? And how many of them is there?
>>
>> Thanks a lot! I'm still seasoning my style of working in R!
>>
>> Regards,
>>
>>      John
>>
>> On Mon, Dec 8, 2008 at 4:10 PM, Greg Snow <[hidden email]> wrote:
>>> 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
>>> [hidden email]
>>> 801.408.8111
>>>
>>>
>>>> -----Original Message-----
>>>> From: tsunhin wong [mailto:[hidden email]]
>>>> Sent: Monday, December 08, 2008 1:34 PM
>>>> To: Greg Snow
>>>> Cc: Jim Holtman; [hidden email]; [hidden email]
>>>> 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 <[hidden email]> 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
>>>> > [hidden email]
>>>> > 801.408.8111
>>>> >
>>>> >
>>>> >> -----Original Message-----
>>>> >> From: [hidden email] [mailto:r-help-bounces@r-
>>>> >> project.org] On Behalf Of tsunhin wong
>>>> >> Sent: Monday, December 08, 2008 8:45 AM
>>>> >> To: Jim Holtman
>>>> >> Cc: [hidden email]
>>>> >> 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 <[hidden email]>
>>>> >> wrote:
>>>> >> > ?get
>>>> >> >
>>>> >> >
>>>> >> > Sent from my iPhone
>>>> >> >
>>>> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]> 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
>>>> >> >>
>>>> >> >> ______________________________________________
>>>> >> >> [hidden email] 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.
>>>> >> >
>>>> >>
>>>> >> ______________________________________________
>>>> >> [hidden email] 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.
>>>> >
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> Antonio, Fabio Di Narzo
> Ph.D. student at
> Department of Statistical Sciences
> University of Bologna, Italy
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Transforming a string to a variable's name? help me newbie...

Greg Snow-2
In reply to this post by tsunhin wong
You can try the TkListView function from the TeachingDemos package.  I have tested it with lists with about 50 elements, but not thousands, so no guarantees.  It opens a new window with the elements of the list in the left pane (can be scrolled up and down), each element then has a '+' next to it that if you click on it will show the next level down of just that element.  There are buttons to further explore (print/str/custom) the currently selected element.

You can also just do str or whatever on single elements:

> str(mylist[[5]])

Will give the details on the 5th element of mylist.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: tsunhin wong [mailto:[hidden email]]
> Sent: Tuesday, December 09, 2008 4:22 PM
> To: Greg Snow
> Cc: [hidden email]
> Subject: Re: [R] Transforming a string to a variable's name? help me
> newbie...
>
> I did really want to find make the suggestion work... it is going to
> save my time...
> I worked on trying to put all dataframes into a list() by:
> ###start of file
> rm(list=ls())
> setwd("/Users/John/Programs/R")
> load("Expt108Master2008.Rdata")
>
> nms <- ls(pattern=".data")
> my.data <- list()
> for(i in nms) my.data[[i]] <- get(i)
>
> save(list=ls(pattern="my."), file="Expt108One.Rdata")
> ###end of file
>
> The original Rdata file (with global dataframes) is 427.4Mb, and the
> new Rdata file with 1 list() storing all dataframes is 427.2Mb
> I can now access the individual dataframes by:
> my.data$019v1msa1.data
>
> But...
> At the moment, whenever I type
> >my.data
> then the whole huge list of dataframes just flush out of standard
> output... and doing a
> >dim(my.data)
> will give me only NULL...
> what command(s) allows me to look at what variables / dataframes are
> stored in a list() structure? And how many of them is there?
>
> Thanks a lot! I'm still seasoning my style of working in R!
>
> Regards,
>
>       John
>
> On Mon, Dec 8, 2008 at 4:10 PM, Greg Snow <[hidden email]> wrote:
> > 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
> > [hidden email]
> > 801.408.8111
> >
> >
> >> -----Original Message-----
> >> From: tsunhin wong [mailto:[hidden email]]
> >> Sent: Monday, December 08, 2008 1:34 PM
> >> To: Greg Snow
> >> Cc: Jim Holtman; [hidden email]; [hidden email]
> >> 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 <[hidden email]>
> 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
> >> > [hidden email]
> >> > 801.408.8111
> >> >
> >> >
> >> >> -----Original Message-----
> >> >> From: [hidden email] [mailto:r-help-bounces@r-
> >> >> project.org] On Behalf Of tsunhin wong
> >> >> Sent: Monday, December 08, 2008 8:45 AM
> >> >> To: Jim Holtman
> >> >> Cc: [hidden email]
> >> >> 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 <[hidden email]>
> >> >> wrote:
> >> >> > ?get
> >> >> >
> >> >> >
> >> >> > Sent from my iPhone
> >> >> >
> >> >> > On Dec 8, 2008, at 7:11, "tsunhin wong" <[hidden email]>
> 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
> >> >> >>
> >> >> >> ______________________________________________
> >> >> >> [hidden email] 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.
> >> >> >
> >> >>
> >> >> ______________________________________________
> >> >> [hidden email] 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.
> >> >
> >

______________________________________________
[hidden email] 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.