Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create new dataframe from existing dataframe based on unique column values in R

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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),]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading