Hello,
I have the following dataset (this is a piece of a much larger set):
RID SCRNO VISCODE RECNO CONTTIME
23 18 HBA0190012 bl 1 5
24 18 HBA0190012 bl 3 5
28 18 HBA0190012 bl 5 5
29 18 HBA0190012 bl 2 5
32 18 HBA0190012 bl 4 5
38 19 HBA0190013 bl 2 35
50 19 HBA0190013 bl 1 5
57 20 HBA0190014 bl 1 10
61 21 HBA0200015 bl 2 30
64 21 HBA0200015 bl 3 90
67 21 HBA0200015 bl 4 90
72 21 HBA0200015 bl 8 2
76 21 HBA0200015 bl 1 60
88 22 HBA0190016 bl 1 25
97 23 HBA0190017 bl 3 85
99 23 HBA0190017 bl 1 5
103 23 HBA0190017 bl 2 5
122 25 HBA0190019 bl 1 20
145 26 HBA0200020 bl 1 60
170 27 HBA0190021 bl 1 3
190 28 HBA0220022 bl 1 40
194 29 HBA0220023 bl 1 25
223 29 HBA0220023 bl 2 25
And I would like to output a list of the sums of CONTTIME for each SCRNO. What
is the best way to do that? Any help would be greatly appreciated.
Thanks!
Edgar
The simplest way is:
xxx <- with(clyde,tapply(CONTTIME,SCRNO,sum))
You could also do:
xxx <-
by(clyde,clyde[["SCRNO"]],function(x){sum(x[["CONTTIME"]])})
but this gives somewhat messy output; the aforesaid output may be
convenient for some purposes, not for others.
cheers,
Rolf Turner
On 30/06/11 09:38, Edgar Alminar wrote:> Hello,
> I have the following dataset (this is a piece of a much larger set):
>
> RID SCRNO VISCODE RECNO CONTTIME
> 23 18 HBA0190012 bl 1 5
> 24 18 HBA0190012 bl 3 5
> 28 18 HBA0190012 bl 5 5
> 29 18 HBA0190012 bl 2 5
> 32 18 HBA0190012 bl 4 5
> 38 19 HBA0190013 bl 2 35
> 50 19 HBA0190013 bl 1 5
> 57 20 HBA0190014 bl 1 10
> 61 21 HBA0200015 bl 2 30
> 64 21 HBA0200015 bl 3 90
> 67 21 HBA0200015 bl 4 90
> 72 21 HBA0200015 bl 8 2
> 76 21 HBA0200015 bl 1 60
> 88 22 HBA0190016 bl 1 25
> 97 23 HBA0190017 bl 3 85
> 99 23 HBA0190017 bl 1 5
> 103 23 HBA0190017 bl 2 5
> 122 25 HBA0190019 bl 1 20
> 145 26 HBA0200020 bl 1 60
> 170 27 HBA0190021 bl 1 3
> 190 28 HBA0220022 bl 1 40
> 194 29 HBA0220023 bl 1 25
> 223 29 HBA0220023 bl 2 25
>
> And I would like to output a list of the sums of CONTTIME for each SCRNO.
What is the best way to do that? Any help would be greatly appreciated.
>
> Thanks!
> Edgar
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>> I did this: >> >> library(data.table) >> >> dd <- data.table(bl) >> dd[,sum(as.integer(CONTTIME)), by = SCRNO] >> >> (I used as.integer because I got an error message: sum not meaningful for factors) >> >> And got this: >> >> SCRNO V1 >> [1,] HBA0020036 111 >> [2,] HBA0020087 71 >> [3,] HBA0020209 140 >> [4,] HBA0020213 189 >> [5,] HBA0020222 174 >> [6,] HBA0020292 747 >> [7,] HBA0020310 57 >> [8,] HBA0020317 291 >> [9,] HBA0020365 417 >> [10,] HBA0020366 124 >> >> All the sums are way too big. Is there something making it not add up correctly? >> >> Original dataset: >>RID SCRNO VISCODE RECNO CONTTIME 338 43 HBA0020036 bl 1 9 1187 95 HBA0020087 bl 1 3 3251 230 HBA0020209 bl 2 3 3258 230 HBA0020209 bl 1 28 3321 235 HBA0020213 bl 2 5 3351 235 HBA0020213 bl 1 6 3436 247 HBA0020222 bl 1 5 3456 247 HBA0020222 bl 2 4 4569 321 HBA0020292 bl 13 2 4572 321 HBA0020292 bl 5 13 4573 321 HBA0020292 bl 1 25 4576 321 HBA0020292 bl 7 5 4578 321 HBA0020292 bl 8 2 4581 321 HBA0020292 bl 4 4 4582 321 HBA0020292 bl 9 5 4586 321 HBA0020292 bl 12 2 4587 321 HBA0020292 bl 6 2 4590 321 HBA0020292 bl 10 3 4591 321 HBA0020292 bl 11 7