bernie2436
bernie2436

Reputation: 23901

How do I select distinct from a dataframe in R?

I have a dataframe in R. I want to see what groups are in the dataframe. If this were a SQL database, I would do Select distinct group from dataframe. Is there a way to perform a similar operation in R?

> head(orl.df)
     long      lat order  hole piece group id
1 3710959 565672.3     1 FALSE     1   0.1  0
2 3710579 566171.1     2 FALSE     1   0.1  0

Upvotes: 0

Views: 1596

Answers (2)

user2925400
user2925400

Reputation: 11

I think the table() function is also a good choice.

table(orl.df$group)

It also tell you the number the items in each group.

Upvotes: 0

David
David

Reputation: 9405

The unique() function should do the trick:

> dat <- data.frame(x=c(1,1,2),y=c(1,1,3))
> dat <- data.frame(x=c(1,1,2),y=c(1,1,3))
> dat
  x y
1 1 1
2 1 1
3 2 3
> unique(dat)
  x y
1 1 1
3 2 3

Edit: For your example (didn't see the group part)

unique(orl.df$group)

Upvotes: 3

Related Questions