I am trying to use the cast()
function from the Reshape package to convert a long format dataframe into wide format. Below is the dataframe (named textMessagesLong) I am working with.
I am using the cast()
function as it's shown in my textbook (Discovering Statistics Using R) as follows: cast(molten data, variables coded within a single column ~ variables coded across many columns, value = "outcome variable")
. which in the case above is cast(textMessagesLong, Group ~ variable, value = "value")
.
I am getting the "Aggregation requires fun.aggregate: length used as default" error.
I tried using the cast()
function as indicated here: https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format, but it didn't resolve the error.
I also found the following thread https://stackoverflow.com/questions/9621401/aggregation-requires-fun-aggregate-length-used-as-default but didn't see they answer there (they are using cast()
and dcast()
)
I looked up ?cast()
which shows different arguments than what's shown in the book.
To understand the issue a little better, I recreated the dataframe in wide format in MS Excel (saved as a .csv file) where I added a column named "Persons" which is an array 1:50. Then I read the file asssigned to a new dataframe (newDataFrame). Only then I was able to switch between long and wide formats using the following two commands:
newDataFrame <- read.table("New Data Frame.csv", header = TRUE)
long <- melt(newDataFrame, id = c("Persons", "Group"), measured = c("Baseline", "Six_Months")
wide <- cast(long, Person + Group ~ variable, value = "value")
I am not sure why adding "Persons" to the dataframe made the difference. Apologies if my question is naive.