0 votes
1 view
ago in R Programming by (32.1k points)

I just want to duplicate the records that are based on >1 column using dplyr / tidyverse

Example

library(dplyr)

df <- data.frame(a=c(1,1,1,2,2,2), b=c(1,2,1,2,1,2), stringsAsFactors = F)

I thought the above command would return rows 3 and 6, but it returns 0 rows.

df %>% filter(duplicated(a, b))

# [1] a b

# <0 rows> (or 0-length row.names)

Conversely, I thought this would return rows 1,2,4 and 5, but it returns all rows.

df %>% filter(!duplicated(a, b))

#   a b

# 1 1 1

# 2 1 2

# 3 1 1

# 4 2 2

# 5 2 1

# 6 2 2

Am I missing something?

1 Answer

0 votes
ago by (70.8k points)

See in the R programming the "duplicated()" function basically works with a vector or a data frame or an array as you can see in the below code:

df %>%

  filter(duplicated(.))

#   a b

# 1 1 1

# 2 2 2

df %>%

  filter(!duplicated(.))

#   a b

# 1 1 1

# 2 1 2

# 3 2 2

# 4 2 1

If you prefer to reference a specific subset of columns, then you can use cbind(), refer to the following code:

df %>%

  filter(duplicated(cbind(a, b)))

Note that  the dplyr for the code can be distinct:

df %>%

  distinct(a, b, .keep_all = TRUE)

#   a b

# 1 1 1

# 2 1 2

# 3 2 2

# 4 2 1

Related questions

+2 votes
1 answer
0 votes
1 answer
asked 6 days ago in R Programming by ashely (32.1k points)
+2 votes
1 answer
asked May 29, 2019 in R Programming by Ritik (3.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...