Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R: crosstable anaysis for NPS calculation in r

$
0
0

i I have a nps survey data in R, the survey raw data look like this:

df <- read.table(
  text = "Gender  Age  Promoters   Passives   Detractors
F       20   0           1          0
M       19   1           0          0
M       21   1           0          0
F       19   0           0          1
M       20   1           0          0
M       18   0           1          0
F       18   1           0          0
F       21   0           0          1
M       19   0           0          1
F       20   0           1          0
F       21   0           0          1   ",
  header = TRUE
)      

Generally nps is calculated as:

(Number of Promoters)/all responses - (Number of Detractors)/all responses)

(0 means no response of course)

Now i would like to calculate nps for for specific age, gender and many other variables... like crosstable. The desire outcome would look like this:

        gender   age
        ------   ------------------
total   F    M   18   19   20   21

0      -0.6   0.7   0.1   -0.1  0.3   -0.2

actually i tried group by

df %>% 
group_by(Gender) %>% 
summarise(nps = (sum(Promoters)-sum(Detractors))/(sum(Promoters)+sum(Passives)+sum(Detractors)))

but it comes out like this:

     age    nps
     0      0
     1      0
     2      0
     3      0

I'm not sure how to do this in R. Any help would be great!


Viewing all articles
Browse latest Browse all 201839

Trending Articles