Tou Mou
Tou Mou

Reputation: 1274

How to extract rows based on a condition?

Good afternoon ,

Assume i have two data frames such as :

   Sensor_location  Target_location detection_Probability
3                11               5             0.2943036
16               15               9             0.2943036
61               19              22             0.2943036
71                4               1             0.2943036
25                8              11             0.2943036
14               10              16             0.2943036 

M1=structure(list(`Sensor_location ` = c(19, 15, 5, 13, 18, 8), 
    Target_location = c(22, 14, 2, 19, 12, 9), detection_Probability = c(0.294303552937154, 
    0.294303552937154, 0.294303552937154, 0.294303552937154, 
    0.294303552937154, 0.294303552937154)), row.names = c(21L, 
45L, 38L, 17L, 3L, 28L), class = "data.frame")

And :

M2=structure(list(c(5L, 16L, 9L, 11L, 17L, 24L, 8L, 6L, 10L, 14L, 
20L, 23L, 15L, 2L, 21L, 22L, 12L, 18L, 19L, 1L, 3L, 7L, 13L, 
24L), c(11L, 22L, 15L, 17L, 23L, 23L, 14L, 12L, 16L, 20L, 23L, 
24L, 21L, 8L, 24L, 23L, 18L, 24L, 22L, 7L, 9L, 13L, 19L, 23L), 
    c(6L, 17L, 12L, 12L, 18L, 21L, 11L, 5L, 11L, 17L, 21L, 22L, 
    18L, 5L, 20L, 19L, 11L, 17L, 20L, 4L, 6L, 10L, 16L, 22L), 
    c(4L, 13L, 8L, 10L, 16L, 18L, 9L, 3L, 7L, 15L, 19L, 20L, 
    14L, 3L, 15L, 16L, 9L, 15L, 13L, 2L, 2L, 8L, 14L, 21L), c(2L, 
    10L, 3L, 8L, 14L, 20L, 7L, 11L, 4L, 13L, 14L, 17L, 9L, 1L, 
    23L, 20L, 6L, 12L, 23L, 10L, 12L, 1L, 7L, 20L)), row.names = c(NA, 
-24L), class = "data.frame")

1   5 11  6  4  2
2  16 22 17 13 10
3   9 15 12  8  3
4  11 17 12 10  8
5  17 23 18 16 14
6  24 23 21 18 20
7   8 14 11  9  7
8   6 12  5  3 11
9  10 16 11  7  4
10 14 20 17 15 13
11 20 23 21 19 14
12 23 24 22 20 17
13 15 21 18 14  9
14  2  8  5  3  1
15 21 24 20 15 23
16 22 23 19 16 20
17 12 18 11  9  6
18 18 24 17 15 12
19 19 22 20 13 23
20  1  7  4  2 10
21  3  9  6  2 12
22  7 13 10  8  1
23 13 19 16 14  7
24 24 23 22 21 20

I'm searching a way to sample rows from M2 such as the first column values (5,9,12,1,11,16) are in M1[["Target_location"]]. An example :

1   5 11  6  4  2
3   9 15 12  8  3
17 12 18 11  9  6
20  1  7  4  2 10
4  11 17 12 10  8
2  16 22 17 13 10

In the case a value like 24 is present in M1[["Target_location"]] , we sample only one possibility from M2 which means :

6  24 23 21 18 20 
or 
24 24 23 22 21 20

I tried with failure:

M2 %>%
    group_by(M2[,1]) %>%
    filter(all(M1[["Target_location"]] %in% M2[,1])) 

I hope my question is clear and feasible. Thank you a lot for your help !

Upvotes: 0

Views: 46

Answers (2)

akrun
akrun

Reputation: 887881

The dataset 'M2' is not a standard data.frame as the column names are NULL

colnames(M2)
#NULL

It may be better to set the names first

subset(setNames(M2, paste0("X", seq_along(M2))), X1 %in% M1$Target_location)

although, extracting the values work for the dataset, but it can fail elsewhere

subset(M2, M2[,1] %in% M1$Target_location)

Upvotes: 1

Rosalie Bruel
Rosalie Bruel

Reputation: 1503

I don't see the value 24 in the "Target location" column, so I am not sure I understood your question.

However, if you want to return all the rows of M2 whose values in the first column are within a "Target location" of M1, the code below will work:

M2 %>% filter(M2[, 1] %in% M1[, "Target_location"])

Upvotes: 1

Related Questions