Home > front end >  Create new dataframe from existing dataframe based on unique column values in R
Create new dataframe from existing dataframe based on unique column values in R

Time:11-04

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.

CodePudding user response:

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),]

CodePudding user response:

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

  • Related