[R] Rename objects based on list

jim holtman jholtman at gmail.com
Sat Nov 15 19:51:28 CET 2008


Here is one way using a 'list':

> x <- data.frame(a=1:10, b=21:30, c=41:50)
> out <- lapply(x, function(z){
+     hist(z, plot=FALSE)
+ })
>
> str(out)
List of 3
 $ a:List of 7
  ..$ breaks     : num [1:6] 0 2 4 6 8 10
  ..$ counts     : int [1:5] 2 2 2 2 2
  ..$ intensities: num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ density    : num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ mids       : num [1:5] 1 3 5 7 9
  ..$ xname      : chr "z"
  ..$ equidist   : logi TRUE
  ..- attr(*, "class")= chr "histogram"
 $ b:List of 7
  ..$ breaks     : num [1:6] 20 22 24 26 28 30
  ..$ counts     : int [1:5] 2 2 2 2 2
  ..$ intensities: num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ density    : num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ mids       : num [1:5] 21 23 25 27 29
  ..$ xname      : chr "z"
  ..$ equidist   : logi TRUE
  ..- attr(*, "class")= chr "histogram"
 $ c:List of 7
  ..$ breaks     : num [1:6] 40 42 44 46 48 50
  ..$ counts     : int [1:5] 2 2 2 2 2
  ..$ intensities: num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ density    : num [1:5] 0.1 0.1 0.1 0.1 0.1
  ..$ mids       : num [1:5] 41 43 45 47 49
  ..$ xname      : chr "z"
  ..$ equidist   : logi TRUE
  ..- attr(*, "class")= chr "histogram"
>


On Sat, Nov 15, 2008 at 1:33 PM, Wade Wall <wade.wall at gmail.com> wrote:
> Hi all,
>
> I am trying to find a way to rename R objects with names pulled from a
> vector of names.  For example, I have a data frame, my.data.frame, and
> a list of names, my.names.  My.names is simply the column names of
> my.data.frame.
>
> I want save the histogram with the column name as the name of the object.
>
> for (i in 1:ncol(my.data.frame) {
>  tmp<-hist(my.data.frame[,i])
> name(tmp)<-my.names[i] ##This is not real code, but what I want to do,
> which is change the name of tmp to the name held at my.names[,i]
> }
>
> Basically, I am looking for a way to loop through columns in a data
> frame, perform some function on the column, and name the output based
> on the name of the column.
>
> Thanks for any help.
>
> Wade
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list