1

So, I have a data frame that looks like this:

plot <- data.frame(plot=c("A", "A", "A", "B", "B", "C", "C", "C"), 
                   grid= c(1,1,1,2,2,3,3,3),
                   year1=c(2000,2000,2010,2000,2010,2000,2010,2010),
                   year2=c(2005,2005,2015,2005,2015,2005,2015,2015))

plot 

  plot grid year1 year2
1    A    1  2000  2005
2    A    1  2000  2005
3    A    1  2010  2015
4    B    2  2000  2005
5    B    2  2010  2015
6    C    3  2000  2005
7    C    3  2010  2015
8    C    3  2010  2015

So for the plot column I have repeated values, the grid is always unique for each of the plots but the years are changing, what I want basically is a new data frame which will just keep all the unique combinations from these four columns, which would look like this:

  plot grid year1 year2
1    A    1  2000  2005
2    A    1  2010  2015
3    B    2  2000  2005
4    B    2  2010  2015
5    C    3  2000  2005
6    C    3  2010  2015

I tried to look for solution but I could not fine anything that fits to my example.

1
  • 1
    unique(plot)? Commented Nov 4, 2022 at 10:05

2 Answers 2

1

Use distinct:

library(dplyr)
distinct(plot)

  plot grid year1 year2
1    A    1  2000  2005
2    A    1  2010  2015
3    B    2  2000  2005
4    B    2  2010  2015
5    C    3  2000  2005
6    C    3  2010  2015

Or in base R, with duplicated:

plot[!duplicated(plot),]
Sign up to request clarification or add additional context in comments.

Comments

1

data.table option using unique:

library(data.table)
unique(setDT(plot))
#>    plot grid year1 year2
#> 1:    A    1  2000  2005
#> 2:    A    1  2010  2015
#> 3:    B    2  2000  2005
#> 4:    B    2  2010  2015
#> 5:    C    3  2000  2005
#> 6:    C    3  2010  2015

Created on 2022-11-04 with reprex v2.0.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.