The error message you're seeing, "Error in log_returnsAsset: operator is invalid for atomic vectors," suggests that the object log_returns is not a data frame (or similar structure like a tibble that supports the $ operator) but an atomic vector. This means you cannot access its elements using the $ operator.
Without knowing the exact structure of the log_returns object, I will assume a common scenario where log_returns is expected to be a data frame containing a column named "Asset" among other columns. In this case, you should ensure that log_returns is indeed a data frame with the required column. To correct the error, you must first ensure or convert log_returns into a data frame (or a suitable structure) with an "Asset" column.
Here's how you might correct it, assuming log_returns originally should be a data frame:
- Ensure
log_returns is Loaded as a Data Frame: Ensure that the data in log_returns is loaded properly as a data frame. For instance, if you are reading from a CSV, you would use read.csv() or read_csv() from the readr package:
# Assuming log_returns is stored in a CSV file
log_returns <- read.csv("path_to_your_log_returns.csv")
# Or using readr for possibly better performance and convenience
library(readr)
log_returns <- read_csv("path_to_your_log_returns.csv")
- Correction If
log_returns is An Atomic Vector: If log_returns is an atomic vector and you're trying to reshape it into a data frame, you'll need additional context or data to correctly structure it, especially how the "Asset" information would be integrated. This could involve combining it with another vector or data frame that includes asset identifiers and then reshaping it into the desired format.
Assuming log_returns was meant to be a data frame and you have it structured correctly, your selection code for the assets should work as intended. If log_returns was loaded in a different format (e.g., a matrix or a different type of object) and should contain information about assets and their log returns, you would need to convert this into a data frame first. Here’s a sketch on how to approach this if your data was in a matrix with the first column being the assets:
# Example of converting from a matrix (or similar structure) to a data frame
# Replace this with your actual data structure transformation
log_returns_df <- data.frame(Asset = log_returns[, 1], LogReturns = log_returns[, -1])
# Then you can run your selections as you attempted
strategy1_assets <- c("T", "PFE", "XAU", "GOOGL", "USDINR")
strategy1_selection <- log_returns_df[log_returns_df$Asset %in% strategy1_assets, ]
strategy2_assets <- c("EURUSD", "NG", "PFE", "GOOGL", "T")
strategy2_selection <- log_returns_df[log_returns_df$Asset %in% strategy2_assets, ]
Without more specific information about the structure and content of log_returns, this guidance assumes it's a typical use case of asset returns analysis. Adjust the transformations as needed based on your actual data and structure.