jarmy
February 14, 2020, 1:48am
1
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
jarmy:
month ~ variable,
Do you maybe mean climate_variable rather than variable?
jarmy
February 14, 2020, 2:19am
3
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 repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
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.
jarmy
February 14, 2020, 3:17am
7
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"))
jarmy
February 14, 2020, 3:18am
8
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.
jarmy
February 14, 2020, 3:41am
10
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)
system
Closed
March 6, 2020, 4:38am
12
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.