Re: [R] how to add row index based a categorical column

2011-05-31 Thread xin wei
thank you everyone. how can I not be aware of the existence of ave()? I try the following: tapply(data$value, data$level, rank). However, I have a very difficult time merging the resulting rank variable back to the original data frame. thanks a lot! -- View this message in context: http://r.78

Re: [R] how to add row index based a categorical column

2011-05-27 Thread Dennis Murphy
Hi: Another angle. Calling your example data frame df, df <- transform(df, Rank = with(df, ave(value, level, FUN = rank))) df[with(df, order(level, value)), ] value level Rank 3 2 A1 1 4 A2 2 5 A3 5 9 B1 410 B2 634 B3 9

Re: [R] how to add row index based a categorical column

2011-05-27 Thread David Winsemius
On May 27, 2011, at 3:12 PM, xin wei wrote: hello, I have the following data manipulation issue. the following is the sample data: value level 4 A 5 A 2 A 10 B 9 B 34 B 100 C 34 C 101C. I hope to get the following result: value level rank 2 A

Re: [R] how to add row index based a categorical column

2011-05-27 Thread Joshua Wiley
Hi, This seems to work (although I have this sense that I am missing something, but I cannot put my finger on it). There are undoubtedly other ways: ## data in a form ready for copy and pasting (created using dput() ) dat <- structure(list(value = c(4L, 5L, 2L, 10L, 9L, 34L, 100L, 34L, 101L), l

[R] how to add row index based a categorical column

2011-05-27 Thread xin wei
hello, I have the following data manipulation issue. the following is the sample data: value level 4 A 5 A 2 A 10 B 9 B 34 B 100 C 34 C 101C. I hope to get the following result: value level rank 2 A 1 4 A 2 5 A 3 9