On Aug 27, 2009, at 8:31 AM, Henrique Dallazuanna wrote:

Try this:

tapply(B, factor(A, levels = seq(max(A))), sum)

Nice! It might have a disadvantage in that it produces NA's instead of the requested 0's, but that would be easily remedied with an:

is.na(obj) <- 0

It was much neater than my hack:

sapply(1:max(A), function(x) ifelse(x %in% A, tapply(B, A, sum) [as.character(x)], 0) )

Which would have neded to be rbind()'ed to 1:max(A) to get the desired construction.

--
David.


On Thu, Aug 27, 2009 at 9:26 AM, Maithili Shiva <maithili_sh...@yahoo.com >wrote:








Dear Sirs,

At the outset I sincerely apologize for reproducing my query to you. I also thank all of you for the solution you had provided. It has worked on the
actual data I am working with.

However, there is this peculiar problem which I had realized only after I
had obtained my results.

e.g. in the example I had attached

A <- c(2, 2, 1, 3, 7, 3, 3, 1, 14, 7, 31)
B <- c(0.0728,0.9538,4.0140,0.0020,2.5593,0.1620,2.513,0.3798,
.0033,0..2282, 0.1614)

tapply( B, A, sum)

I get R output as –

1          2          3            7            14          31
4.3938   1.0266   2.6770    2.7875    0.0033    0.1614

However, my requirement is I should get the output as

1          2          3         4  5  6  7         8  9
. 14


..31
4.3938 1.0266  2.6770   0  0  0  2.7875  0  0 ..0.0033 .. 0.161

i.e. my output should include the values 4, 5, 6, etc. which are not part of dataset A and the corresponding totals in B (which are anyways 0’s). I need this for my further analysis. Its possible for me to add these 0’s
manually, however when the dataset is large, its not practical.

I am attaching herewith an excel file. I will be grateful if you can guide
me.

Thanks in advance

Maithili
start: 2009-08-18 end: 0000-00-00

Thinking of ordering food? Find restaurant numbers on Yahoo! India Local


    Love Cricket? Check out live scores, photos, video highlights and
more. Click here http://cricket.yahoo.com
______________________________________________
R-help@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.




--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to