Reputation: 43
I am doing time series analysis on oil prices in R. In order to complete one of my modeling tasks, a MONTH column next to the closing price of oil each month was added:
df <- cbind(df, MONTH=1:nrow(df))
The output was successful and came out to this:
OILSTOCK MONTH
1 1965 1
2 1812 2
3 1492 3
4 1289 4
5 1256 5
I then have to take a holdout sample on the last 12 months of the data (there are a total of 108 months), I do so with the following:
n_OILSTOCK=OILSTOCK[1:96]
n_MONTH=MONTH[1:96]
The command above works as expected for n_OILSTOCK, but for some reason, I get the following error in regards to n_MONTH:
> n_MONTH=MONTH[1:96]
Error: object 'MONTH' not found
I am unsure why R is not recognizing this new MONTH column as a proper object. Is there an easy way for me to address this? The original data was brought in as .txt format. Thank you for your assistance.
Upvotes: 1
Views: 1505
Reputation: 9603
MONTH
doesn't exist in your environment. It is just a column you created in the df
Data Frame when you called cbind(df, MONTH=1:nrow(df))
.
To reference MONTH
as a column of this dataframe, you can do it this way:
n_MONTH <- df$MONTH[1:96]
Alternatively:
n_MONTH <- df[1:96, 'MONTH']
As explained, this is because MONTH
doesn't exist as an object. You have to reference it by pointing it to the column in your df
dataframe.
In comparison, OILSTOCK
actually do exist in your environment, and you're able to perform the subsetting operation without a problem.
Upvotes: 2