Skip to main content
Remove new question; spelling/grammar/capitalization; remove tags from title
Source Link
Joshua Ulrich
  • 177.3k
  • 33
  • 357
  • 429

how to call Extract values from an indexed data frame / xts ? robject based on dates in data.frame rownames

I am doing a multi period-period analysis and iI need to extract values between two dates from a table based on results.

The values are in a time series xtsan xts object. i will call this named data.

The start and beginningend of my calculation lies between those dates. These same dates are used as rownames somewhere in the xtsa data.frame named dataresults file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the colcolumn names with this vector:

>col_names> col_names
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is callextract the valuesobservations in the data file which lie atobject in the columns in col_names and between start_date and end_date

EDIT

I thank akrun for the code bellow. It provides part of the solution to my problem.

some data:

 library(xts)
    data(sample.xts)
    start_date <- '2007-01-02'
    end_date <- '2007-01-05' 
    col_names <-  c('Open', 'High')

The code bellow grabs the values within that range.

sample.xts[paste(start_date, end_date, sep="/"), col_names]

What i really need is the next 5 values for example. so akrun suggested this:

 sample.xts[paste(as.Date(start_date) + 5, as.Date(end_date) + 5, sep="/"), col_names]

The problem is that i have trading days so that excludes not only weekends but also holidays etc.. so i would like to make the above arbitrary of calendar days which is the format of as.Date. I need the next 10 values from that table rather than the next 10 calendar days.

how to call values from an indexed data frame / xts ? r

I am doing a multi period analysis and i need to extract values from a table based on results.

The values are in a time series xts object. i will call this data

The start and beginning of my calculation lies between those dates. These same dates are used as rownames somewhere in the xts data file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the col names with this vector:

>col_names 
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is call the values in the data file which lie at col_names and between start_date and end_date

EDIT

I thank akrun for the code bellow. It provides part of the solution to my problem.

some data:

 library(xts)
    data(sample.xts)
    start_date <- '2007-01-02'
    end_date <- '2007-01-05' 
    col_names <-  c('Open', 'High')

The code bellow grabs the values within that range.

sample.xts[paste(start_date, end_date, sep="/"), col_names]

What i really need is the next 5 values for example. so akrun suggested this:

 sample.xts[paste(as.Date(start_date) + 5, as.Date(end_date) + 5, sep="/"), col_names]

The problem is that i have trading days so that excludes not only weekends but also holidays etc.. so i would like to make the above arbitrary of calendar days which is the format of as.Date. I need the next 10 values from that table rather than the next 10 calendar days.

Extract values from an xts object based on dates in data.frame rownames

I am doing a multi-period analysis and I need to extract values between two dates from an xts object named data.

The start and end of my calculation lies between those dates. These same dates are used as rownames in a data.frame named results.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the column names with this vector:

> col_names
[1] "Stock1" "Stock2" "Stock3" 

I need to extract the observations in the data object in the columns in col_names and between start_date and end_date.

the previous question only provided part of the solution so i added that information to the body of my question
Source Link
Alex Bădoi
  • 830
  • 2
  • 10
  • 24

I am doing a multi period analysis and i need to extract values from a table based on results.

The values are in a time series xts object. i will call this data

The start and beginning of my calculation lies between those dates. These same dates are used as rownames somewhere in the xts data file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the col names with this vector:

>col_names 
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is call the values in the data file which lie at col_names and between start_date and end_date

EDIT

I thank akrun for the code bellow. It provides part of the solution to my problem.

some data:

 library(xts)
    data(sample.xts)
    start_date <- '2007-01-02'
    end_date <- '2007-01-05' 
    col_names <-  c('Open', 'High')

The code bellow grabs the values within that range.

sample.xts[paste(start_date, end_date, sep="/"), col_names]

What i really need is the next 5 values for example. so akrun suggested this:

 sample.xts[paste(as.Date(start_date) + 5, as.Date(end_date) + 5, sep="/"), col_names]

The problem is that i have trading days so that excludes not only weekends but also holidays etc.. so i would like to make the above arbitrary of calendar days which is the format of as.Date. I need the next 10 values from that table rather than the next 10 calendar days.

I am doing a multi period analysis and i need to extract values from a table based on results.

The values are in a time series xts object. i will call this data

The start and beginning of my calculation lies between those dates. These same dates are used as rownames somewhere in the xts data file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the col names with this vector:

>col_names 
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is call the values in the data file which lie at col_names and between start_date and end_date

I am doing a multi period analysis and i need to extract values from a table based on results.

The values are in a time series xts object. i will call this data

The start and beginning of my calculation lies between those dates. These same dates are used as rownames somewhere in the xts data file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the col names with this vector:

>col_names 
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is call the values in the data file which lie at col_names and between start_date and end_date

EDIT

I thank akrun for the code bellow. It provides part of the solution to my problem.

some data:

 library(xts)
    data(sample.xts)
    start_date <- '2007-01-02'
    end_date <- '2007-01-05' 
    col_names <-  c('Open', 'High')

The code bellow grabs the values within that range.

sample.xts[paste(start_date, end_date, sep="/"), col_names]

What i really need is the next 5 values for example. so akrun suggested this:

 sample.xts[paste(as.Date(start_date) + 5, as.Date(end_date) + 5, sep="/"), col_names]

The problem is that i have trading days so that excludes not only weekends but also holidays etc.. so i would like to make the above arbitrary of calendar days which is the format of as.Date. I need the next 10 values from that table rather than the next 10 calendar days.

Source Link
Alex Bădoi
  • 830
  • 2
  • 10
  • 24

how to call values from an indexed data frame / xts ? r

I am doing a multi period analysis and i need to extract values from a table based on results.

The values are in a time series xts object. i will call this data

The start and beginning of my calculation lies between those dates. These same dates are used as rownames somewhere in the xts data file.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the col names with this vector:

>col_names 
[1] "Stock1", "Stock2", "Stock3" 

what I need to do is call the values in the data file which lie at col_names and between start_date and end_date