This question is related to this question My question is about R: How to number each repetition in a table in R?
Where basically the repetitions are numbered. E.g two repetitions: 1,2 ; three repetitions: 1,2,3 etc... But if the value is unique (only one time) it should have not 1
but NA
data: (from akrun, many thanks!)
df1 <- structure(list(Fullname = c("Peter", "Peter", "Alison", "Warren",
"Jack", "Jack", "Jack", "Jack", "Susan", "Susan", "Henry", "Walison",
"Tinder", "Peter", "Henry", "Tinder")), row.names = c(NA, -16L
), class = "data.frame")
my solution would be this:
df1 %>%
group_by(Fullname) %>%
mutate(newcol = seq_along(Fullname))
Fullname newcol
<chr> <int>
1 Peter 1
2 Peter 2
3 Alison 1
4 Warren 1
5 Jack 1
6 Jack 2
7 Jack 3
8 Jack 4
9 Susan 1
10 Susan 2
11 Henry 1
12 Walison 1
13 Tinder 1
14 Peter 3
15 Henry 2
16 Tinder 2
Now I try to set each value that occurs once (e.g. Alison, Warren and Henry) to NA
like akrun did here My question is about R: How to number each repetition in a table in R?
My code is with a ifelse
statement checking if the sum of the group is >1.
df1 %>%
group_by(Fullname) %>%
mutate(newcol = seq_along(Fullname)) %>%
mutate(newcol = ifelse(sum(newcol)>1, newcol, NA))
but I get:
Fullname newcol
<chr> <int>
1 Peter 1
2 Peter 1
3 Alison NA
4 Warren NA
5 Jack 1
6 Jack 1
7 Jack 1
8 Jack 1
9 Susan 1
10 Susan 1
11 Henry 1
12 Walison NA
13 Tinder 1
14 Peter 1
15 Henry 1
16 Tinder 1
And I can't grasp why?
Aucun commentaire:
Enregistrer un commentaire