Garry Hazell
Garry Hazell

Reputation: 21

R question on variable assignment involving a date

I am new to R and trying to combine data via a simple read from the CBOE and quantmod data from Yahoo. I am have a few problems with understanding how R works. I am reading SKEW data from the CBOE (Chicago Board of Options) via:

skew <- read.csv("http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/Skewdailyprices.csv",skip=1,header=TRUE,stringsAsFactors=F)

The first column of data is a date in mm/dd/yyyy format. Then I tried to convert this to a date:

skew.dte <- as.Date(skew[,1],format="%m/%d/%Y") 

which works but, if I then do:

head(skew)    
      Date   SKEW    
1 1/2/1990 126.09     
2 1/3/1990 123.34    
3 1/4/1990 122.62    
4 1/5/1990 121.27    
5 1/8/1990 124.12    
6 1/9/1990 119.82

My question is, why is dte not part of the dataframe skew? I thought the skew.dte would do this.

Upvotes: 2

Views: 850

Answers (2)

PaulHurleyuk
PaulHurleyuk

Reputation: 8169

As Conjugate stated, you need to use a $ to access items within an object (here a dataframe), or the [ operator. See the R user manual Chapter 6 for more

skew$dte <- as.Date(skew$Date,format="%m/%d/%Y") 

or

skew[,'dte'] <- as.Date(skew[,'Date'],format="%m/%d/%Y") 

(or any mixture thereof)

Upvotes: 3

nico
nico

Reputation: 51670

R allows points in variable names, often used in lieu of a space to separate words in the name of a variable

skew.dte <- as.Date(skew[,1],format="%m/%d/%Y") 

assigns values to the variable called skew.dte.

If you want to add the values to the data frame use

skew$dte <- as.Date(skew[,1],format="%m/%d/%Y") 

Upvotes: 4

Related Questions