Home > Back-end >  Get unique rows of a data frame (using `expand.grid`?)
Get unique rows of a data frame (using `expand.grid`?)

Time:08-09

df <- data.frame(x=rep(c("A","B","C"),each=4),z=rep(c(1,2),each=2,times=3), c1=rep(c("u","w","v"),each=4))

   x z c1
1  A 1  u
2  A 1  u
3  A 2  u
4  A 2  u
5  B 1  w
6  B 1  w
7  B 2  w
8  B 2  w
9  C 1  v
10 C 1  v
11 C 2  v
12 C 2  v

I'm trying to use expand.grid to achieve this result:

  x z c1
1 A 1 u
2 B 1 w
3 C 1 v
4 A 2 u
5 B 2 w
6 C 2 v

I tried with(df, expand.grid(x=unique(x),z=unique(z),c1=unique(c1))) but this leads to:

   x z c1
1  A 1  u
2  B 1  u
3  C 1  u
4  A 2  u
5  B 2  u
6  C 2  u
7  A 1  w
8  B 1  w
9  C 1  w
10 A 2  w
11 B 2  w
12 C 2  w
13 A 1  v
14 B 1  v
15 C 1  v
16 A 2  v
17 B 2  v
18 C 2  v

CodePudding user response:

Besides unique(df), as provided by Darren Tsai in the comments, you can use dplyr::distinct:

dplyr::distinct(df)

  x z c1
1 A 1  u
2 B 1  w
3 C 1  v
4 A 2  u
5 B 2  w
6 C 2  v

CodePudding user response:

Expand only the first two vectors, then create the 3rd column. R will recycle c1 to the appropriate length.

df <- data.frame(x=rep(c("A","B","C"),each=4),
                 z=rep(c(1,2),each=2,times=3), 
                 c1=rep(c("u","w","v"),each=4))

df2 <- with(df, expand.grid(x=unique(x),z=unique(z)))
df2$c1 <- unique(df$c1)
df2
#>   x z c1
#> 1 A 1  u
#> 2 B 1  w
#> 3 C 1  v
#> 4 A 2  u
#> 5 B 2  w
#> 6 C 2  v

Created on 2022-08-09 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related