jeudi 27 août 2015

dplyr-how to insert NA values when the column sort order changes

I'm trying to figure out a simple way to insert NA values when the column values are changed from the decreasing order to mixed order. But if decreasing order continues logically after this mixed ordered rows, its ok to keep this rows too.

If the column does not have decreasing order row values replace values with NA.

Any column which can keep the sort sequence at least 5 numbers can pass. Else replace NA values.

Its best for me to do this process with dplyr . I tried to do this but cannot came up with the idea:(.

dt_new <- dt%>%
    mutate_each(funs(replace(., which(ifelse(.....

example data

C1 = c(1:10,7,8,11,12)
C2 = c(2:12,7,13,12)
C3 = sample(1:14)
C4 = c(1:14)

dt <- data.frame(C1,C2,C3,C4)

       C1 C2 C3 C4 C5
#   1   1  2  4  1  1
#   2   2  3 12  2  2
#   3   3  4  6  3  8
#   4   4  5 10  4  5
#   5   5  6  7  5  7
#   6   6  7  5  6  4
#   7   7  8 13  7  6
#   8   8  9 14  8  3
#   9   9 10  8  9  9
#   10 10 11  1 10  5
#   11  7 12  9 11  6
#   12  8  7  2 12  7 
#   13 11 13  3 13  8
#   14 12 12 11 14 10

the output Which I look for

       C1 C2 C3 C4 C5
#   1   1  2 NA  1 NA
#   2   2  3 NA  2 NA
#   3   3  4 NA  3 NA 
#   4   4  5 NA  4 NA
#   5   5  6 NA  5 NA
#   6   6  7 NA  6 NA
#   7   7  8 NA  7 NA
#   8   8  9 NA  8 NA
#   9   9 10 NA  9 NA
#   10 10 11 NA 10 5
#   11 NA 12 NA 11 6
#   12 NA NA NA 12 7
#   13 11 13 NA 13 8
#   14 12 NA NA 14 10

Aucun commentaire:

Enregistrer un commentaire