简体   繁体   English

如何根据条件提取行?

[英]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"]] .我正在寻找一种从M2中采样行的方法,例如第一列值(5,9,12,1,11,16)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:如果M1[["Target_location"]]中存在像24这样的值,我们只从M2中采样一种可能性,这意味着:

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 !非常感谢您的帮助!

I don't see the value 24 in the "Target location" column, so I am not sure I understood your question.我在“目标位置”列中没有看到值24 ,所以我不确定我是否理解您的问题。

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 的所有行,其第一列中的值在 M1 的“目标位置”内,下面的代码将起作用:

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

The dataset 'M2' is not a standard data.frame as the column names are NULL数据集“M2”不是标准数据框,因为列名是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM