dataframe - Using R - Compare differend columns of data frames to a set of valid column combinations -


this question has answer here:

i'm using r studio r version 3.2.2 , want filter rows in data frame, want see rows, combination of values 2 columns don't match valid combination stored value pairs in second data frame. i've heard r not designed use loops in performant way i'm looking solution without using loops, if there any.

using sample data scaled down, first data frame dfitem contains item ids , 2 statuses.

> dfitem   id status_1 status_2 1  1        1        1 2  2        1        2 3  3        2        1 4  4        3        3 5  5        2        3 6  6        1        1 

second data frame dfvalidpairs contains valid combinations of 2 statuses.

> dfvalidstatus   status_1 status_2 1        1        1 2        1        2 3        3        3 

how can check if status pair dfitem, consisting of status_1 , status_2, in 1 of valid statuses dfvalidpairs? in end have rows dfitem have invalid status pair, stored in new data frame this.

> dfinvalid   id status_1 status_2 1  3        2        1 2  5        2        3  

i can think of loops iterate on data frames. know if there more performant possibilities?

cheers, felix


code objects

> dfitem <- data.frame( c(1, 2, 3, 4, 5, 6), c(1, 1, 2, 3, 2, 1), c(1, 2, 1, 3, 3, 1) ) > colnames( dfitem ) <- c( "id", "status_1", "status_2" ) > dfvalidstatus <- data.frame( c(1, 1, 3), c(1,2,3) ) > colnames( dfvalidstatus ) <- c( "status_1", "status_2" ) 

this type of merge - or here opposite:

> library(dplyr) > anti_join(dfitem, dfvalidstatus)  joining by: c("status_1", "status_2")   id status_1 status_2 1  5        2        3 2  3        2        1 

the other useful merge or set operations inner_join, left_join, right_join etc


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -