[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 category D. The new matrix should
be like this:


  Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C 80 1
D 120 1  D 130 2

Please, could anyone help me with this problem?

Thank you.
-- 
Marcos Martins Santos
mmsantos...@gmail.com
--
Skype: marcosmartinssantos

[[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.


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 mmsantos...@gmail.comwrote:

 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 category D. The new matrix should
 be like this:


   Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C 80 1
 D 120 1  D 130 2

 Please, could anyone help me with this problem?

 Thank you.
 --
 Marcos Martins Santos
 mmsantos...@gmail.com
 --
 Skype: marcosmartinssantos

 [[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.


[[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.


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 Djurovic djandr...@gmail.comwrote:

 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 mmsantos...@gmail.comwrote:

 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 category D. The new matrix should
 be like this:


   Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C 80 1
 D 120 1  D 130 2

 Please, could anyone help me with this problem?

 Thank you.
 --
 Marcos Martins Santos
 mmsantos...@gmail.com
 --
 Skype: marcosmartinssantos

 [[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.




[[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.


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 djandr...@gmail.com:

 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 Djurovic djandr...@gmail.comwrote:

 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 mmsantos...@gmail.comwrote:

 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 category D. The new matrix
 should
 be like this:


   Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C 80
 1
 D 120 1  D 130 2

 Please, could anyone help me with this problem?

 Thank you.
 --
 Marcos Martins Santos
 mmsantos...@gmail.com
 --
 Skype: marcosmartinssantos

 [[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.






-- 
Marcos Martins Santos
mmsantos...@gmail.com
--
Tel. (85) 3044-7436
Cel. (85) 9636-6612
--
Skype: marcosmartinssantos

[[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.


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 djandr...@gmail.com 
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), 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 Djurovic djandr...@gmail.comwrote:

 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 mmsantos...@gmail.comwrote:

 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 category D. The new matrix should
 be like this:


   Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C 80 1
 D 120 1  D 130 2

 Please, could anyone help me with this problem?

 Thank you.
 --
 Marcos Martins Santos
 mmsantos...@gmail.com
 --
 Skype: marcosmartinssantos

         [[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.




    [[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.


__
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.


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 smartpink...@yahoo.com:

 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 djandr...@gmail.com
 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), 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 Djurovic djandr...@gmail.com
 wrote:

  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 mmsantos...@gmail.com
 wrote:
 
  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 category D. The new matrix
 should
  be like this:
 
 
Category Observed_value Index  A 100 1  A 130 2  A 140 3  B 90 1  C
 80 1
  D 120 1  D 130 2
 
  Please, could anyone help me with this problem?
 
  Thank you.
  --
  Marcos Martins Santos
  mmsantos...@gmail.com
  --
  Skype: marcosmartinssantos
 
  [[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.
 
 
 

 [[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.




-- 
Marcos Martins Santos
mmsantos...@gmail.com
--
Tel. (85) 3044-7436
Cel. (85) 9636-6612
--
Skype: marcosmartinssantos

[[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.