Quantcast

count value changes in a column

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

count value changes in a column

Justin Haynes
is there a way to look for value changes in a column?

set.seed(144)
df<-data.frame(state=sample(rep(1:5,200),1000))

any of the five states are acceptable.  however if, for example,
states 4 or 5 follow state 3, i want to overwrite them with 3.
changes from 1 to any value and 2 to any value are acceptable as are
changes from any value to 1 or 2.

By way of an example:

the sequence 1 3 3 5 5 3 2 4 2 1 5 3 3 5

should read   1 3 3 3 3 3 2 4 2 1 5 5 5 5


Thanks for the help!

Justin

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: count value changes in a column [EDIT]

Justin Haynes
Justin Haynes <jtor14 <at> gmail.com> writes:

>
> is there a way to look for value changes in a column?
>
> df<-data.frame(state=sample(rep(1:5,200),1000))
>
> any of the five states are acceptable.  however if, for example,
> states 4 or 5 follow state 3, i want to overwrite them with 3.
> changes from 1 to any value and 2 to any value are acceptable as are
> changes from any value to 1 or 2.
>
> By way of an example:
>
> the sequence 1 3 3 5 5 3 2 4 2 1 5 3 3 5
>
> should read   1 3 3 3 3 3 2 4 2 1 5 5 5 5
>

Additionally, can i count the number of times my vector changes to a number?  
in my previous example i would like to see
state count

1    2
2    2
3    1
4    1
5    1

> Thanks for the help!
>
> Justin
>
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: count value changes in a column

jholtman
In reply to this post by Justin Haynes
Why isn't the sequence:

1 3 3 3 3 3 2 4 2 1 5 3 3 3

according to your rule about 5s following 3s.

On Tue, May 31, 2011 at 6:23 PM, Justin Haynes <[hidden email]> wrote:

> is there a way to look for value changes in a column?
>
> set.seed(144)
> df<-data.frame(state=sample(rep(1:5,200),1000))
>
> any of the five states are acceptable.  however if, for example,
> states 4 or 5 follow state 3, i want to overwrite them with 3.
> changes from 1 to any value and 2 to any value are acceptable as are
> changes from any value to 1 or 2.
>
> By way of an example:
>
> the sequence 1 3 3 5 5 3 2 4 2 1 5 3 3 5
>
> should read   1 3 3 3 3 3 2 4 2 1 5 5 5 5
>
>
> Thanks for the help!
>
> Justin
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: count value changes in a column

Bill.Venables
I thought so to.  If so, here is one way you could do it

fixSeq <- function(state) {
  shift1 <- function(x) c(1, x[-length(x)])
  repeat {
    change <- state %in% c(4,5) & shift1(state) == 3
    if(any(change))
        state[change] <- 3 else break
  }
  state
}

e.g.
> state
 [1] 1 3 3 5 5 3 2 4 2 1 5 3 3 5
> fixSeq(state)
 [1] 1 3 3 3 3 3 2 4 2 1 5 3 3 3
>

For the data frame:
> set.seed(144)
> dfr <- data.frame(state = sample(rep(1:5,200)))
>
> dfr <- within(dfr, changed <- fixSeq(state))
> head(dfr, 11)
   state changed
1      5       5
2      2       2
3      1       1
4      5       5
5      2       2
6      1       1
7      1       1
8      4       4
9      3       3
10     5       3  ### <<--- change
11     3       3
>  

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of jim holtman
Sent: Wednesday, 1 June 2011 11:20 AM
To: Justin Haynes
Cc: [hidden email]
Subject: Re: [R] count value changes in a column

Why isn't the sequence:

1 3 3 3 3 3 2 4 2 1 5 3 3 3

according to your rule about 5s following 3s.

On Tue, May 31, 2011 at 6:23 PM, Justin Haynes <[hidden email]> wrote:

> is there a way to look for value changes in a column?
>
> set.seed(144)
> df<-data.frame(state=sample(rep(1:5,200),1000))
>
> any of the five states are acceptable.  however if, for example,
> states 4 or 5 follow state 3, i want to overwrite them with 3.
> changes from 1 to any value and 2 to any value are acceptable as are
> changes from any value to 1 or 2.
>
> By way of an example:
>
> the sequence 1 3 3 5 5 3 2 4 2 1 5 3 3 5
>
> should read   1 3 3 3 3 3 2 4 2 1 5 5 5 5
>
>
> Thanks for the help!
>
> Justin
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: count value changes in a column

Justin Haynes
I apologize for the confusion but that solution will work with a twist.

I want to record only the first value of a state change that goes above 2.
so if the sequence is

344455544334 it should read all 3s

but 3442555414433 should read 3332555514444


Hope that helps clarify, if not I can get there from your function Bill,

Thanks!

Justin

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...