[R] Data Frame Column Name Attribute

David Winsemius dwinsemius at comcast.net
Sat Apr 23 22:15:33 CEST 2016


> On 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.

The attribute comes from the name of the argument to `apply`

> 
> 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))

The `rbind` function is returning a matrix and `[[<-. data.frame` is adding it to the tdata object. Matrices have dimname attributes. It appears to me that you have also earlier attempted (successfully) to add a column named "OpenInt" higher up in your code that you have not shown us.

If you want to get rid of this column use `[<-.data.frame` with a NULL argument

tdata['OpenInt'] <- NULL


If you start again with neither of these columns, I would suggest this instead:

tdata[["newcol2"]] <- c( 0, apply(tdata["Open"],2,diff ) )

The valued returned should be an undimensioned object and will not get bound to the dataframe as a matrix.

-- 
David.
> 
>> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list