R-how to compare previous row value with present row value -


how compare previous row value present row value grouping column id,q.name.

my data frame ,

 id     q.name       name         country   gender    1                jack         in               male         2                jhon         au               male   1       view         shane        usa              male         1       view         rose         uk               female   2       upload       rose         au               female            3                guna         in               male   4                guna         in               male   4                na           jp               male 

test ex:

id     q.name        name       country   gender             namechanged    countrychanged     genderchanged    1                jack         in               male          0               0                     0   1       view         shane        usa              male          0               0                     0    1       view         rose         uk               female        1               1                     1   2                jhon         au               male          0               0                     0   2       upload       rose         au               female        0               0                     0         3                guna         in               male          0               0                     0   4                guna         in               male          0               0                     0     4                na           jp               male          1               1                     0 

my expected data frame be,

 id    q.name  namechangedcount   nameunchangedcount   countrychangedcount   countryunchanged    genderchangedcount  genderunchangedcount   totalid    1            0                  1                      0                      1                 0                       1                  1   1      view      1                  1                      1                      1                 1                       1                  2   2            0                  1                      0                      1                 0                       1                  1   2     upload     0                  1                      0                      1                 0                       1                  1   3            0                  1                      0                      1                 0                       1                  1   4            1                  1                      1                      1                 0                       2                  2 

dput data.

structure(list(id = c(1, 2, 1, 1, 2, 3, 4, 4), q.name = structure(c(1l,  1l, 3l, 3l, 2l, 1l, 1l, 1l), .label = c("get", "upload", "view" ), class = "factor"), name = structure(c(2l, 3l, 5l, 4l, 4l,  1l, 1l, na), .label = c("guna", "jack", "jhon", "rose", "shane" ), class = "factor"), country = structure(c(2l, 1l, 5l, 4l, 1l,  2l, 2l, 3l), .label = c("au", "in", "jp", "uk", "usa"), class = "factor"),      gender = structure(c(2l, 2l, 2l, 1l, 1l, 2l, 2l, 2l), .label = c("female",      "male"), class = "factor")), .names = c("id", "q.name", "name",  "country", "gender"), row.names = c(na, -8l), class = "data.frame") 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -