Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slice reoccuring values using dplyr

Tags:

r

dplyr

taking the following data

A <- c(4,4,4,5,5,5,5,6,6)
B <- c(1,2,3,1,3,4,3,2,7)

data1 <- data.frame(A,B)

I want to remove the duplicated B values for each A.

So my new table should remove data1[7,]

I want to use the dplyr() package And have tried the following code

data2 <- data1 %>% 
  group_by(A) %>% 
  filter(slice(B(1))) 

Can someone help me with the correct filter() command

like image 426
lukeg Avatar asked Feb 12 '26 02:02

lukeg


1 Answers

You can try

library(dplyr)
data1 %>%
     group_by(A) %>% 
     filter(!duplicated(B))#or
     #slice(which(!duplicated(B)))
like image 148
akrun Avatar answered Feb 16 '26 20:02

akrun