vendredi 27 janvier 2017

combining ifelse statement mutate and sequence in dplyr pipe

I want to replace cities names in a D3.js bubble chart like this one How to display d3 bubbles in different colors for a dataset with one branch and many children?

I have a lot of "small cities" to visualise and I want to replace their labels by an id. for better display (and create a legend).

Here's a small exemple

cities <- c("Brest", "Rennes", "Rennes", "Rennes", "Nantes", "Lorient") 
dataset <- data.frame(cities)  
dataset

dataset <- dataset %>%
count(cities)

Here's my result: n = 1 (1 = "small cities")

cities     1
Brest      1
Lorient    1
Nantes     1
Rennes     3

small cities =

What I expect:

A sequence of id's for cities with n = 1 (1 = "small cities")

cities     n     id_sequence
Brest      1     1
Lorient    1     2
Nantes     1     3
Rennes     3     NA

I am trying to complete my pipe without success with :

dataset <- dataset %>%
   count(cities) %>% 
   mutate (id_sequence = ifelse (n = 1:length(cities))

Thank you for helping!

Aucun commentaire:

Enregistrer un commentaire