[R] Help Required in using cast (reshape package) function

Punit Anand punit.anand at lusight.com
Wed Nov 21 17:20:48 CET 2007


Hello everyone,

I am new to R. I have data in the form of excel pivot table format and I
want to cast it into a format which can make it compatible with
computation. 

Since I already have the package in pivot format; I avoid melt function
and use the cast directly. 
I inspect the 

dataread <- read.csv(".....", header=TRUE)

Data in the format 
Id		Region        	Country	Industry
Period	variable		value
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "account payable" 10000
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "account receivable" 50000
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "XXXX" 	70000
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "YYYY" "NA"
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "ZZZZ" 
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "AAAA" 10000
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"   "ccccccc" 10000

# I want to arrange data in the format
Id		Region        	Country	Industry
Period	"account payable"	"account receivable"	XXXX"	"YYYYY"
"ZZZZ"	"AAAA"	"CCCCC"
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"    10000	50000		70000	 "NA"		10000
10000		

# casting the data
cast <- cast(dataread, ID + Period ~ variable)

When I do that the data is casted as a pivot with a warning "Aggregation
requires fun.aggregate: length used as default", and the casted data
gives me the count of variables (as suggested by the warning )

Id		Region        	Country	Industry
Period	"account payable"	"account receivable"	XXXX" 	"YYYYY"
"ZZZZ"	"AAAA"	"CCCCC"
"Aaa11  xx"  "Latin America" 	"Mexico"	"food & beverages"
"2002:FY"    1	1	1	 NA	NA		10000
10000		

How do I use the fun.aggregate feature of the cast function to obtain
the desired result?

Moreover, I want to subdivide the casted data into subsets based on ids.
How do I achieve that?


Thanks in advance,

Punit Anand



More information about the R-help mailing list