[R] Data Frame Column Name Attribute

William Dunlap wdunlap at tibco.com
Sat Apr 23 22:17:29 CEST 2016


You could use transform() instead of [[<- to add columns to your data.frame
so the new columns get transformed they way they do when given to the
data.frame function itself.  E.g.,

> dd <- data.frame(X=1:5, Y=11:15)
> str(transform(dd, Z=matrix(X+Y,ncol=1,dimnames=list(NULL, "NewZ"))))
'data.frame':   5 obs. of  3 variables:
 $ X   : int  1 2 3 4 5
 $ Y   : int  11 12 13 14 15
 $ NewZ: int  12 14 16 18 20
> str(transform(dd, Z=matrix(c(X,Y),ncol=2,dimnames=list(NULL,
c("NewZ1","NewZ2")))))
'data.frame':   5 obs. of  4 variables:
 $ X      : int  1 2 3 4 5
 $ Y      : int  11 12 13 14 15
 $ Z.NewZ1: int  1 2 3 4 5
 $ Z.NewZ2: int  11 12 13 14 15



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Apr 23, 2016 at 8:59 AM, thomas mann <twiniverse2000 at gmail.com>
wrote:

> I am attempting to add a calculated column to a data frame.  Basically,
> adding a column called "newcol2" which are the stock closing prices from 1
> day to the next.
>
> The one little hang up is the name of the column.  There seems to be an
> additional data column name included in the attributes (dimnames?).  So
> when i run HEAD(DATAFRAMENAME) i get the column name = "Open".   but you
> can see below... it is called "newcol2 with some attribute called open.
>
> I did a lot of google searches but really could not find what i was looking
> for.  Any help is greatly appreciated.  Thank you!
>
> tdata[["newcol2"]] <- rbind(0,apply(tdata["Open"],2,diff))
>
> > str(tdata)
> 'data.frame': 7505 obs. of  8 variables:
>  $ Date   : int  19860709 19860710 19860711 19860714 19860715 19860716
> 19860717 19860718 19860721 19860722 ...
>  $ Open   : num  9.14 9.45 9.3 9.08 8.71 ...
>  $ High   : num  9.45 9.51 9.35 9.08 8.71 ...
>  $ Low    : num  9.03 9.14 9.03 8.65 7.96 ...
>  $ Close  : num  9.4 9.35 9.03 8.76 8.5 ...
>  $ Volume : int  332719 180049 234212 218772 605427 339507 306866 418593
> 94880 119332 ...
>  $ OpenInt: num [1:7505, 1] 0 0.317 -0.155 -0.223 -0.367 ...
>   ..- attr(*, "dimnames")=List of 2
>   .. ..$ : NULL
>   .. ..$ : chr "Open"
> * $ newcol2:* num [1:7505, 1] 0 0.317 -0.155 -0.223 -0.367 ...
>   ..- attr(*, "dimnames")=List of 2
>   .. ..$ : NULL
> *  .. ..$ : chr "Open"*
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list