user79928
user79928

Reputation: 11

Subsetting in R with multiple criteria

I have a dataset as follows (in data.frame format):

>dataset
X Y Z Value
a c f 12
a d f 45
a c f 654
a d g 684
a c g 54
b d f 78
b c f 31
b d f 777
b c g 54 
b d g 45

And I have an second data.frame with criteria:

>criteria
X Y Z 
a c f 
b d f 

How do I apply the second matrix to the first to get, in this example, c(654, 12, 777, 68) as a result? Most of the things I've tried end up pulling out all the lines with any of the three variables matching instead of all three.

EDIT: Fixed what the result is supposed to be

Upvotes: 1

Views: 121

Answers (3)

joel.wilson
joel.wilson

Reputation: 8413

adding some points on y logic :

do.call(paste0, criteria)
# [1] "acf" "bdf"
do.call(paste0, dataset[1:3])
# [1] "acf" "adf" "acf" "adg" "acg" "bdf" "bcf" "bdf" "bcg" "bdg"
v = do.call(paste0, dataset[1:3]) %in% do.call(paste0, criteria)
# [1]  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE

Now this logical vector is used to subset the Value column

dataset$Value[v]
# [1]  12 654  78 777

Upvotes: 0

akrun
akrun

Reputation: 887098

We can use the tidyverse

library(tidyverse)
inner_join(df1, df2) %>%
             select(Value)

Upvotes: 0

Paulo MiraMor
Paulo MiraMor

Reputation: 1610

Just use merge:

merge(df1, df2)

If you want just the vector:

merge(df1, df2)[,'Value']

Data:

df1 <- read.table(text = 
'X Y Z Value
a c f 12
a d f 45
a c f 654
a d g 684
a c g 54
b d f 78
b c f 31
b d f 777
b c g 54 
b d g 45', h = T)

df2 <- read.table(text = '
X Y Z 
a c f 
b d f', h = T)

Upvotes: 3

Related Questions