0

With the RWE below, I am one step short of achieving my desired result. I can identify the unique combinations, but I want to obtain a data.table of all three columns (A,B and C)

a <- c(1,1,1,1,1,2,3,4,5,5,5,6,6,7,7,8,8,9,9,9,9,9,10,10,10)
b <- c(1,2,1,1,5,5,6,1,1,1,3,2,2,1,1,2,3,1,2,3,4,4,1,2,2)
c <- c(1,1,3,1,1,2,3,4,5,5,5,6,6,7,7,8,8,9,9,9,9,9,10,10,20)
df1 <- data.frame(a, b, c)
head(df1)
#
library(dplyr) # example using dplyr package
df1 %>%
  distinct(a,b)
#
unique( df1[ , 1:2 ] ) # using base R

These commands return the unique combinations of a AND b from df1, but I want the output to include the c values in the data.frame also.

1
  • df1[!duplicated(df1[, 1:2]), ] ? Commented Jun 15, 2017 at 10:15

1 Answer 1

1

We need to use .keep_all as TRUE. In the default, it is FALSE

library(dplyr)
df1 %>% 
   distinct(a, b, .keep_all = TRUE)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.