Walter Anderson
2012-Mar-01 17:23 UTC
[R] Need help using Melt and cast to compute correlation for a cross tabulation
I have a data frame with a number of observed and predicted values by classification as shown below: Count Volume FCLASS 1 55000 60000 Grade Separated 2 43000 39000 Grade Separated 3 26000 26500 Major Arterial 4 19500 20000 Major Arterial ... There are four classes here: Grade Separated, Major Arterial, Minor Arterial, and Collector I am looking to compute the following information FCLASS cor(Count,Volume) Grade Separated 0.999 Major Arterial 0.999 Minor Arterial 0.999 Collector 0.999 I am attempting to use the following commands to achieve this: library(reshape) tmp <- melt(dataframe) cast(tmp, FCLASS ~ variable, function(Count, Volume) cor(Count, Volume)) but this is generating: Error in is.data.frame(y) : argument "Volume" is missing, with no default Any suggestions? Walter Anderson
jim holtman
2012-Mar-01 17:42 UTC
[R] Need help using Melt and cast to compute correlation for a cross tabulation
Here is another way of doing it:> x <- read.table(text = " Count Volume FCLASS+ 1 55000 60000 'Grade Separated' + 2 43000 39000 'Grade Separated' + 3 26000 26500 'Major Arterial' + 4 19500 20000 'Major Arterial'", as.is = TRUE)> result <- sapply(split(x, x$FCLASS), function(.grp) cor(.grp$Count,.grp$Volume))> > resultGrade Separated Major Arterial 1 1>On Thu, Mar 1, 2012 at 12:23 PM, Walter Anderson <wandrson01@gmail.com>wrote:> I have a data frame with a number of observed and predicted values by > classification as shown below: > > Count Volume FCLASS > 1 55000 60000 Grade Separated > 2 43000 39000 Grade Separated > 3 26000 26500 Major Arterial > 4 19500 20000 Major Arterial > ... > > There are four classes here: Grade Separated, Major Arterial, Minor > Arterial, and Collector > > I am looking to compute the following information > > FCLASS cor(Count,Volume) > Grade Separated 0.999 > Major Arterial 0.999 > Minor Arterial 0.999 > Collector 0.999 > > I am attempting to use the following commands to achieve this: > > library(reshape) > tmp <- melt(dataframe) > cast(tmp, FCLASS ~ variable, function(Count, Volume) cor(Count, Volume)) > > but this is generating: Error in is.data.frame(y) : argument "Volume" is > missing, with no default > > Any suggestions? > > Walter Anderson > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]