[R] Converting a character string into a data frame name and performing assignments to that data frame

Charles C. Berry cberry at tajo.ucsd.edu
Wed Apr 7 19:28:34 CEST 2010


On Wed, 7 Apr 2010, Ivan Calandra wrote:

> Hi,
> I'm not sure what your goal is, but why don't you use a list?
>
> You can store each new dataframe name in a new element of the list.
> At the end, if each element has the same length, you can use as.data.frame() 
> or something similar.

Using a list would be my first choice.

You can also use an environment, which gives you access to list like 
idioms, or which you can attach() to access the objects from the search 
list:

> my.dfs <- new.env()
> dfname <- 'a.df'
> assign(dfname, as.data.frame(diag(2)),env=my.dfs)
> dfname <- 'b.df'
> assign(dfname, as.data.frame(diag(3)),env=my.dfs)
> ls(my.dfs)
[1] "a.df" "b.df"
> sapply(my.dfs,function(x) coef(lm(V1~.,x)))
$b.df
(Intercept)          V2          V3
           1          -1          -1

$a.df
(Intercept)          V2
           1          -1

> 
HTH,

Chuck
>
> Since you didn't provide any sample data, I'm not sure if that's really what 
> you're looking for, but what I said might get you started.
> I'm no expert, but I might be able to help you a bit further if you provide 
> the necessary information (which you can find in the posting guide)
>
> Ivan
>
> Le 4/7/2010 18:13, Kavitha Venkatesan a écrit :
>>  Hi,
>>  I'd greatly appreciate any insight into the problem in my previous
>>  message below: Please note that I would like to loop through the
>>  following steps for a list of data frames whose names are dynamically
>>  assigned at run time, so I have a character string variable, called
>>  "variable.df", which stores the name of the data frame at any given
>>  point ...and I need to be able to manipulate the contents of the data
>>  frame somehow using this character string :
>>
>> 
>> >  colnames(variable.df) = colnames(some.other.df)
>> >  variable.df = rbind(variable.df, some.other.df)
>> >  write.table(variable.df, file=some.file)
>> >
>>  Thanks!
>>  Kavitha
>>
>>  On Sat, Mar 20, 2010 at 5:07 PM, Kavitha Venkatesan
>>  <kavitha.venkatesan at gmail.com>  wrote:
>> 
>> >  Hi,
>> > 
>> >  I would like to do the following operations:
>> > 
>> >  variable.df is a character string that contains the name of the data
>> >  frame that I want to do the following operations on:
>> > 
>> >  variable.df<- data.frame();
>> >  # I can do the above command using
>> >  assign( variable.df, data.frame() )
>> > 
>> >  How can I perform the assignment statements below ?
>> > 
>> >  colnames(variable.df) = colnames(some.other.df)
>> >  variable.df = rbind(variable.df, some.other.df)
>> >  write.table(variable.df, file=some.file)
>> > 
>> >  Doing
>> >  eval(  substitute( colnames(var), list(var=as.name(variable.df) ) ) )
>> >    didn't get me all the way there.
>> > 
>> >  Thanks!
>> >  Kavitha
>> > 
>> >
>>  ______________________________________________
>>  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.
>>
>> 
>
> -- 
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> ivan.calandra at uni-hamburg.de
>
> **********
> http: //www.for771.uni-bonn.de
> http: //webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
> ______________________________________________
> 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.
>
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list