[R] Assoociative array?
rkevinburton at charter.net
rkevinburton at charter.net
Tue Jul 15 14:33:12 CEST 2008
Sure.
I have the following command:
sc <- split(x, list(x$Category, x$SubCategory), drop=TRUE)
where x is
x <- read.csv("Sales2007.dat", header=TRUE)
and the first few rows are:
DayOfYear,Quantity,Fraction,Category,SubCategory
1,82,0.0000390392720794458,"(Unknown)","(Unknown)"
2,78,0.0000371349173438631,"(Unknown)","(Unknown)"
3,112,0.0000533219325963163,"(Unknown)","(Unknown)"
4,155,0.0000737937460038306,"(Unknown)","(Unknown)"
5,157,0.0000747459233716219,"(Unknown)","(Unknown)"
and str(sc) returns something like:
. . . .
$ T/C.X MEN 3 :'data.frame': 29 obs. of 5 variables:
..$ DayOfYear : int [1:29] 31 37 49 57 63 66 68 78 79 83 ...
..$ Quantity : int [1:29] 1 2 1 1 2 1 1 1 1 1 ...
..$ Fraction : num [1:29] 4.76e-07 9.52e-07 4.76e-07 4.76e-07 9.52e-07 ...
..$ Category : Factor w/ 46 levels "(Unknown)","10\" Plates",..: 40 40 40 40 40 40 40 40 40 40 ...
..$ SubCategory: Factor w/ 246 levels "(Unknown)","70's Disco",..: 246 246 246 246 246 246 246 246 246 246 ...
$ Thank You.X MEN 3 :'data.frame': 8 obs. of 5 variables:
..$ DayOfYear : int [1:8] 37 66 240 246 276 287 316 351
..$ Quantity : int [1:8] 4 2 2 1 2 1 13 3
..$ Fraction : num [1:8] 1.90e-06 9.52e-07 9.52e-07 4.76e-07 9.52e-07 ...
..$ Category : Factor w/ 46 levels "(Unknown)","10\" Plates",..: 41 41 41 41 41 41 41 41
..$ SubCategory: Factor w/ 246 levels "(Unknown)","70's Disco",..: 246 246 246 246 246 246 246 246
Just printing 'sc' gives me something like:
. . .
$`Pinatas/Accessories.Wiggles`
DayOfYear Quantity Fraction Category SubCategory
111180 61 1 4.760887e-07 Pinatas/Accessories Wiggles
111181 88 1 4.760887e-07 Pinatas/Accessories Wiggles
111182 91 1 4.760887e-07 Pinatas/Accessories Wiggles
111183 99 2 9.521774e-07 Pinatas/Accessories Wiggles
111184 102 1 4.760887e-07 Pinatas/Accessories Wiggles
. . .
Now basically I want to know how to iterate throwgh sc. I can print out sc[[1]] etc. from R console and get something like:
> sc[[1]]
DayOfYear Quantity Fraction Category SubCategory
1 1 82 3.903927e-05 (Unknown) (Unknown)
2 2 78 3.713492e-05 (Unknown) (Unknown)
3 3 112 5.332193e-05 (Unknown) (Unknown)
4 4 155 7.379375e-05 (Unknown) (Unknown)
And I know the length of sc is
> length(sc)
[1] 2415
So basically I want to do some kind of summary statistics for each of these groups from 1 to 2415. I have tried this and it doesn't work but I want something like
for(i in 1:length(sc)
{
mean(sc[[i]]$Quantity)
}
I would like similar statistics for median, mode, variance, range, etc. for each group
Thank you
Kevin
---- jim holtman <jholtman at gmail.com> wrote:
> I am not sure what your data looks like. Do 'str(sc)' to show the
> structure. I created a set and this is what I got:
>
> > x <- list(sc,sc,sc,sc)
> > x
> [[1]]
> DayOfYear Quantity Fraction Category SubCategory
> 1 1 82 3.903927e-05 (Unknown) (Unknown)
> 2 2 78 3.713492e-05 (Unknown) (Unknown)
> 3 3 112 5.332193e-05 (Unknown) (Unknown)
>
> [[2]]
> DayOfYear Quantity Fraction Category SubCategory
> 1 1 82 3.903927e-05 (Unknown) (Unknown)
> 2 2 78 3.713492e-05 (Unknown) (Unknown)
> 3 3 112 5.332193e-05 (Unknown) (Unknown)
>
> [[3]]
> DayOfYear Quantity Fraction Category SubCategory
> 1 1 82 3.903927e-05 (Unknown) (Unknown)
> 2 2 78 3.713492e-05 (Unknown) (Unknown)
> 3 3 112 5.332193e-05 (Unknown) (Unknown)
>
> [[4]]
> DayOfYear Quantity Fraction Category SubCategory
> 1 1 82 3.903927e-05 (Unknown) (Unknown)
> 2 2 78 3.713492e-05 (Unknown) (Unknown)
> 3 3 112 5.332193e-05 (Unknown) (Unknown)
>
> > x[[1]]
> DayOfYear Quantity Fraction Category SubCategory
> 1 1 82 3.903927e-05 (Unknown) (Unknown)
> 2 2 78 3.713492e-05 (Unknown) (Unknown)
> 3 3 112 5.332193e-05 (Unknown) (Unknown)
> > x[[1]]$Category
> [1] (Unknown) (Unknown) (Unknown)
> Levels: (Unknown)
> >
>
> not sure exactly what you are working with or trying to do with it.
> If the data is not too large, paste the results of 'dput(sc)' in the
> email so people can easily reconstruct your data.
>
More information about the R-help
mailing list