Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df <- data.frame(
- gr=gl(3,5),
- percent = c(seq(0.1, 0.5,0.1), seq(-0.5,-0.1,0.1), seq(0.1, 0.5,0.1)),
- per=rep(c(0.25,-0.25,0.25),each=5),
- x=c(c(0,0,0,1,2), c(0,0.1,0,1,2),c(1,1,0,0.1,0)))
- > df
- gr percent per x
- 1 1 0.1 0.25 0.0
- 2 1 0.2 0.25 0.0
- 3 1 0.3 0.25 0.0
- 4 1 0.4 0.25 1.0
- 5 1 0.5 0.25 2.0
- 6 2 -0.5 -0.25 0.0
- 7 2 -0.4 -0.25 0.1
- 8 2 -0.3 -0.25 0.0
- 9 2 -0.2 -0.25 1.0
- 10 2 -0.1 -0.25 2.0
- 11 3 0.1 0.25 1.0
- 12 3 0.2 0.25 1.0
- 13 3 0.3 0.25 0.0
- 14 3 0.4 0.25 0.1
- 15 3 0.5 0.25 0.0
- data_manip <- function(x,per,percent){
- if(all(percent>0)&all(head(x,3)==0)&!isTRUE(per<percent&any(diff(x>0)))){
- "Good"
- }
- else
- if(all(percent<0)&isTRUE(per<percent&any(diff(x>0)))){
- "Dirty"
- }
- else
- if(all(percent>0)&isTRUE(per>percent&any(diff(x>0)))){
- "Dirty"
- }
- else
- NA
- }
- library(dplyr)
- df %>%
- group_by(gr)%>%
- do(data.frame(.,eastwood=data_manip(.$x,.$per,.$percent)))
- # A tibble: 15 x 5
- # Groups: gr [3]
- gr percent per x eastwood
- <fctr> <dbl> <dbl> <dbl> <fctr>
- 1 1 0.1 0.25 0.0 Good
- 2 1 0.2 0.25 0.0 Good
- 3 1 0.3 0.25 0.0 Good
- 4 1 0.4 0.25 1.0 Good
- 5 1 0.5 0.25 2.0 Good
- 6 2 -0.5 -0.25 0.0 <NA>
- 7 2 -0.4 -0.25 0.1 <NA>
- 8 2 -0.3 -0.25 0.0 <NA>
- 9 2 -0.2 -0.25 1.0 <NA>
- 10 2 -0.1 -0.25 2.0 <NA>
- 11 3 0.1 0.25 1.0 <NA>
- 12 3 0.2 0.25 1.0 <NA>
- 13 3 0.3 0.25 0.0 <NA>
- 14 3 0.4 0.25 0.1 <NA>
- 15 3 0.5 0.25 0.0 <NA>
Add Comment
Please, Sign In to add comment