Q:

Distinct in r within groups of data

How do I transform a dataframe (on the left) to dataframe (on the right)?

I am trying to do this via dplyr, by grouping into name and distinct, but it gives only 3 rows
df %>%
group_by(name) %>%
distinct(.,keep.all = T) %>%
View()

A:

There is a simple way to access all the cells you want to change:
data <- data.frame(name = c(rep("A", 5), rep("B", 5), rep("C", 5)), subject = c(rep(1:5, 3)), marks = sample(1:100, 15))

> data
   name subject marks
1     A       1    31
2     A       2    12
3     A       3    29
4     A       4    67
5     A       5    99
6     B       1    77
7     B       2     3
8     B       3    92
9     B       4    69
10    B       5    42
11    C       1    52
12    C       2    66
13    C       3    98
14    C       4    23
15    C       5    72

duplicated(data$name) accesses the relevant cells. But R has no way to leave a cell "blank", so to speak.
You can either set them NA, or fill it with an empty character:
data$name[duplicated(data$name)] <- NA

> data
   name subject marks
1     A       1    31
2  <NA>       2    12
3  <NA>       3    29
4  <NA>       4    67
5  <NA>       5    99
6     B       1    77
7  <NA>       2     3
8  <NA>       3    92
9  <NA>       4    69
10 <NA>       5    42
11    C       1    52
12 <NA>       2    66
13 <NA>       3    98
14 <NA>       4    23
15 <NA>       5    72

data$name <- as.character(data$name)
data$name[duplicated(data$name)] <- ""

> data
   name subject marks
1     A       1    30
2             2    52
3             3     5
4             4    48
5             5    99
6     B       1    14
7             2    20
8             3    34
9             4    55
10            5    53
11    C       1    38
12            2    27
13            3    67
14            4    12
15            5    77

To use the latter solution with a factor variable, you need to add "" as a factor label:
data$name <- factor(as.numeric(data$name), 1:4, c(levels(data$name), ""))
data$name[duplicated(data$name)] <- ""

