Re: [R] Creating a new column with the number of the observation

2014-05-28 Thread Marcos Santos
Perfect! Thank you a lot! 2014-05-28 21:57 GMT-03:00 arun : > Hi, > If it ordered by the variable "x", you could also try: > > > within(DF1, new_c<-sequence(table(x))) > > > A.K. > > > > > On Wednesday, May 28, 2014 6:14 PM, Andrija Djurovic > wrote: > Here is another approach: > > x <- rep(c

Re: [R] Creating a new column with the number of the observation

2014-05-28 Thread arun
Hi, If it ordered by the variable "x", you could also try:  within(DF1, new_c<-sequence(table(x))) A.K. On Wednesday, May 28, 2014 6:14 PM, Andrija Djurovic wrote: Here is another approach: x <- rep(c("A", "B", "C"), c(3,1,2)) DF1 <- data.frame(x) cbind(DF1, new_c=ave(as.numeric(DF1$x),

Re: [R] Creating a new column with the number of the observation

2014-05-28 Thread Marcos Santos
Absolutely fantastic! Thank you for the TWO approachs. 2014-05-28 19:13 GMT-03:00 Andrija Djurovic : > Here is another approach: > > x <- rep(c("A", "B", "C"), c(3,1,2)) > DF1 <- data.frame(x) > cbind(DF1, new_c=ave(as.numeric(DF1$x), DF1$x, FUN=function(x) > 1:length(x))) > > Note the differen

Re: [R] Creating a new column with the number of the observation

2014-05-28 Thread Andrija Djurovic
Here is another approach: x <- rep(c("A", "B", "C"), c(3,1,2)) DF1 <- data.frame(x) cbind(DF1, new_c=ave(as.numeric(DF1$x), DF1$x, FUN=function(x) 1:length(x))) Note the difference between DF (in previous solution) and DF1 str(DF) str(DF1) Andrija On Thu, May 29, 2014 at 12:03 AM, Andrija Dju

Re: [R] Creating a new column with the number of the observation

2014-05-28 Thread Andrija Djurovic
Hi. Here is one approach: x <- rep(c("A", "B", "C"), c(3,1,2)) DF <- data.frame(x, stringsAsFactors=FALSE) cbind(DF, new_c=c(lapply(rle(DF$x)[[1]], function(x) 1:x), recursive=T)) Andrija On Wed, May 28, 2014 at 10:46 PM, Marcos Santos wrote: > I have a "data frame" like this: > > Category O

[R] Creating a new column with the number of the observation

2014-05-28 Thread Marcos Santos
I have a "data frame" like this: Category Observed_value A 100 A 130 A 140 B 90 C 80 D 120 D 130 I need to create an index column that show the number of the observation for each category. I have three observations in the category A, one in category B, one in category C and two in catego