R: Compare and filter two data based on conditions

I have data.frame

data_qual

one that looks like this:

data_qual <- structure(list(NAME = structure(1:3, .Label = c("NAME1", "NAME2", "NAME3"), class = "factor"), ID = c(56L, 47L, 77L), YEAR = c(1990L, 2007L, 1899L), VALUE = structure(c(2L, 1L, 1L), .Label = c("ST", "X"), class = "factor")), .Names = c("NAME", "ID", "YEAR", "VALUE"), class = "data.frame", row.names = c(NA, -3L))

 NAME   ID YEAR    VALUE
1 NAME1 56 1990     X
2 NAME2 47 2007    ST
3 NAME3 77 1899    ST

      

I would like to filter the values ​​from data_qual

by comparing it to another dataframe dat

:

dat <- structure(list(NAME = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("NAME1","NAME2"), class = "factor"), ID = c(56L, 56L, 56L, 47L, 47L, 47L, 47L), YEAR = c(1988L, 1989L, 1991L, 2005L, 2006L, 2007L, 2008L), VALUE = c(45L, 28L, 28L, -12L, 14L, 23L, 32L)), .Names = c("NAME", "ID", "YEAR", "VALUE"), class = "data.frame", row.names = c(NA, -7L))

  NAME  ID YEAR   VALUE
1 NAME1 56 1988    45
2 NAME1 56 1989    28
3 NAME1 56 1991    28
4 NAME2 47 2005   -12
5 NAME2 47 2006    14
6 NAME2 47 2007    23
7 NAME2 47 2008    32

      

How can I filter data_qual

based on the column ID

so that in the first filtering process only the rows are written to the new data.frame

one that has a mapping ID

to dat

?

   NAME ID YEAR VALUE
1 NAME1 56 1990     X
2 NAME2 47 2007    ST

      

Then after that I am looking for a way that only rows should be written out of the resulting data.frame that do not have the same YEAR

for each group (as defined ID

)

   NAME ID YEAR VALUE
1 NAME1 56 1990     X

      

Any help is welcome.

+3


source to share


1 answer


For the first part

dat2 <- data_qual[data_qual$ID %in% dat$ID, ]
dat2
   NAME ID YEAR VALUE
1 NAME1 56 1990     X
2 NAME2 47 2007    ST

      

And then for the second part



good_rows <- lapply(paste(dat2$ID, dat2$YEAR, sep = ":"), grepl, x = paste(dat$ID, dat$YEAR, sep = ":"))
dat3 <- dat2[!unlist(lapply(good_rows, any)), ]

      

Or, if this is too messy for you, the for loop

good_rows <- vector(length = nrow(dat2))
for (i in 1:nrow(dat2)) {
   good_rows[i] <- !any(grepl(dat2$YEAR[i], dat[dat$ID == dat2$ID[i], "YEAR"]))
}
dat3 <- dat2[good_rows, ]
dat3
   NAME ID YEAR VALUE
1 NAME1 56 1990     X

      

+2


source