Reputation: 37
i am trying to subset a dataframe with extracted data from another dataframe. My data looks essentially like this:
PriceData
Date AAPL BAC CAT JNJ PG UNH VZ
2004-04-26
2004-04-27 Daily Prices
2004-04-28
2004-04-29
Companies
Date X1 X2 X3 X4
2004-04-26 AAPL CAT PG VZ
2004-04-27 AAPL CAT UNH VZ
2004-04-28 AAPL JNJ UNH VZ
2004-04-29 AAPL JNJ UNH VZ
I have already managed to subset the Companies
dataframe by date, so i have a dataframe with one row containing the tickers of a specific date. For instance:
Tickers
Date X1 X2 X3 X4
2004-04-28 AAPL JNJ UNH VZ
What i would like to do now is to subset the PriceData
dataframe so that only the price data of the four tickers in Tickers
remain:
newPriceData
Date AAPL JNJ UNH VZ
2004-04-26
2004-04-27 Daily Prices
2004-04-28
2004-04-29
Does anybody know what package/function i could use to achieve this?
Upvotes: 0
Views: 61
Reputation: 1438
Base R, data.table, and dplyr solutions. data.table and dplyr are packages which provide alternative functions for data manipulation. I encourage you to look into both -- each is much more flexible and convenient than base R subsetting. You can find the CRAN pages here and here. They each have a vignette (tutorial) listed as "Introduction to ".
Below I've listed a corresponding command for each. For dplyr and data.table, there are slight variations of those listed which might be considered more natural for dplyr or data.table, but these contain the smallest diversions from a similarity to base syntax.
columns_to_retain <- unlist(Tickers[1,-1]) # Take the first row, drop the first column, convert to plain vector.
NewPriceData <- PriceData[,columns_to_retain]
# dplyr
library(dplyr)
NewPriceData <- select(PriceData, columns_to_retain) # or names(Tickers) instead of columns_to_retain
# data.table
NewPriceData <- PriceData[,columns_to_retain, with = FALSE]
Upvotes: 1