Reshape2- dcast problem

Having a problem running dcast in Reshape2.

AirQLong <- melt(data = airquality,id.vars = c("Month","Day"), variable.name = "climate_variable", value.name = "climate_value",na.rm = TRUE)
AirQLong

acast(AirQLong, Day ~ Month ~ climate_variable)
acast(AirQLong, Month ~ climate_variable, mean)
acast(AirQLong, Month ~ climate_variable, mean, margins = TRUE)

AirQWide <-dcast(AirQLong, month ~ variable, mean, fun.aggregate = c("Month", "climate_variable"))

getting "Error in FUN(X[[i]], ...) : object 'variable' not found" everytime. Can anyone please help solve my dcast formula

Do you maybe mean climate_variable rather than variable?

i have tried both ways and the error still shows up. heres a screenshot on my work

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Does this help?

Not really, that is a screenshot, we can't copy your code and data from there to try to reproduce your issue and give you a solution.
Please follow the link I gave you and try to make a proper reproducible example.

library(reshape2)

df <- melt(data = airquality,id.vars = c("Month","Day"), variable.name = "climate_variable", value.name = "climate_value",na.rm = TRUE)
head(df)

AirQWide <-dcast(AirQLong, month ~ climate_variable, mean, fun.aggregate = c("Month", "climate_variable"))

Is that what you are asking for?

Please read the guide in the link, you are not providing sample data. If you take the time to read it, you will notice is actually pretty simple to do.

library(reshape2)

df <- data.frame(stringsAsFactors = FALSE, Month= c(5,5,5,5,5), Day= c(1,2,3,4,6), climate_variable= c("Ozone","Ozone","Ozone","Ozone","Ozone"),climate_value= c(41,36,12,18,28))
df

AirQWide <-dcast(AirQLong, month ~ climate_variable, mean, fun.aggregate = c("Month", "climate_variable"))

Is this what you are trying to do?

library(reshape2)

df <- data.frame(stringsAsFactors = FALSE,
                 Month= c(5,5,5,5,5),
                 Day= c(1,2,3,4,6),
                 climate_variable= c("Ozone","Ozone","Ozone","Ozone","Ozone"),
                 climate_value= c(41,36,12,18,28))

dcast(df, Month ~ climate_variable, mean)
#> Using climate_value as value column: use value.var to override.
#>   Month Ozone
#> 1     5    27

Created on 2020-02-14 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.