I am trying to convert my dataset from wide to long format using dcast
. I start by reading in my file as a CSV (read_SelectionModel
). The code that I have been trying is below:
x = colnames(read_SelectionModel[,3:241])
t = melt(read_SelectionModel, id.vars = c("Ratio", "Ticker"), measure.vars = x, variable.name = "Dates", value.name = "Ratio Value")
r_traverse2 = dcast(t, Dates + Ticker ~ Ratio, value.var = "Ratio Value", fun.aggregate = sum, na.rm = TRUE)
I have two id.vars
variables Ratio and Ticker in order to have each ratio correspond to each stock's ticker. Having measure.vars = x
, and variable.name = "Dates"
. I am creating a new column called "Ratio Value" that will hold the data for each ratio/stock.
Using r_traverse2
I have been using dcast
to try to go from wide to format but I have been getting the following error:
"Error in .fun(.value[0], ...) : invalid 'type' (character) of argument".
I have tried to adjust fun.aggregate
but keep getting the same error as well as the fun
function not being found. How can I go about fixing this?
I have tried fun.aggregate = sum
, fun.aggregate = mean
, and I have also tried not specifying fun.aggregate argument
. If I do not specify this, it defaults to a length where my observations are 0's and 1's instead of the ratio/stock data I want.