1

I have two different data frame showing questionnaire-results from 2014 and 2015, both with the same columns. I want to add the unique rows (i,e. ID-codes) from 2014 to the data frame from 2015.

The problem is that all rows are unique (since there is 23 columns), but I only wish to add the rows containing an unique ID-code (one of the columns), i.e. people that answered the questionnaire 2014 but not 2015.

Using rbind.data.frames succeedes in creating an "2014-2015 dataframe" but then I want to erase the rows containg ID-codes that answered both 2014 and 2015 and keeo those from 2015.

  • 2
    It is much easier to answer a question if you provide reproducible example data, e.g. using `dput()` and your expected output rather than a lot of text. And everyone likes it when you include some code what you have already tried so far. – Roman Oct 10 '16 at 12:36

2 Answers2

1

If I interpret you correctly:

df2014<-structure(list(ID = 1:7, V1 = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 1L), .Label = c("A", "B", "C"), class = "factor"), V2 = structure(c(2L, 
1L, 2L, 1L, 2L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
V3 = structure(c(1L, 1L, 2L, 3L, 3L, 3L, 1L), .Label = c("A", 
"B", "C"), class = "factor")), .Names = c("ID", "V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -7L))

df2015<-structure(list(ID = 4:10, V1 = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 1L), .Label = c("A", "B", "C"), class = "factor"), V2 = structure(c(2L, 
1L, 2L, 1L, 2L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
V3 = structure(c(1L, 1L, 2L, 3L, 3L, 3L, 1L), .Label = c("A", 
"B", "C"), class = "factor")), .Names = c("ID", "V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -7L))

library(dplyr)
rbind(dplyr::filter(df2014, !ID %in% intersect(df2014$ID, df2015$ID)), df2015)

The resulting data frame

    ID V1 V2 V3
1   1  A  B  A
2   2  A  A  A
3   3  A  B  B
4   4  A  B  A
5   5  A  A  A
6   6  A  B  B
7   7  B  A  C
8   8  B  B  C
9   9  C  C  C
10 10  A  A  A
Raphael Lee
  • 111
  • 1
  • 10
  • Thanks Raphael Lee. I guess I confused you by my long text, I should have inserted an data-example myself, sorry for it! However your way resulted in ID 4-7 to me removed. My wish was to keep ID 4-7 but only them from 2015, that is to say. Only one answer per ID and if the same ID sent in answers both 2014 and 2015, keep the newest one. Thanks! – Hampus Persson Oct 10 '16 at 18:04
  • My apologies for mis-interpeting your requirements. I've edited the code. – Raphael Lee Oct 11 '16 at 00:11
  • But that was my fault, Raphael. My english isn't the very best. Tried the new code of yours and it all worked perfectly. Tried some random samples afterwards which proved that only unique asnwers from 2014 had been added to the data frame of 2015. Thanks a lot Raphael! – Hampus Persson Oct 11 '16 at 10:08
0

Use data.table and unique:

library(data.table)
library(magrittr)

# Example data
set.seed(1)
x2014 <- data.table(ID = 1:10, y = rnorm(10))
x2015 <- data.table(ID = 1:10 + 5, y = rnorm(10)) 

# So IDs 6-10 are duplicate

rbind(x2015, x2014) %>%
  setkey(ID) %>%
  unique

Note the order of rbind is important. 2015 must be first if you want that to be table from which rows are kept.

Hugh
  • 15,521
  • 12
  • 57
  • 100
  • Thanks Hugh, I guess you method would work. But I dont really get what to %>% means and does? Tried R-Studios help function without any result. On google I found that %>% was called pipe function but not how it worked. What to write inside the unique(), the data frame or just the "ID" or "dataframe$ID? Thanks in advance! – Hampus Persson Oct 10 '16 at 18:18
  • See https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html for an introduction to the magrittr `%>%` or 'pipe'. But the `%>% `is just to make the code more readable -- although it seems that was counterproductive! I could rewrite it without `%>%`; however, I honestly believe you would find it more instructive to learn about `%>%` and then try to understand this method. – Hugh Oct 10 '16 at 23:06
  • Thanks for the link, think I will have good help from that later on. Thanks for your help and have a good one! – Hampus Persson Oct 11 '16 at 10:11