简体   繁体   English

在 r 中使用多个逻辑标准进行子集化

[英]Using multiple logical criteria for subsetting in r

I have a dataset (loadings from an efa) with three variables/factors (ML1, ML2, ML3) and I would like to extract the names of cases that have an absolute value of >= 0.3 for exactly one variable/factor.我有一个包含三个变量/因素(ML1、ML2、ML3)的数据集(来自 efa 的加载),我想提取绝对值 >= 0.3 的案例名称,恰好是一个变量/因素。

This is what I worked out so far:这是我到目前为止的工作:

items <- row.names(loadings1[(abs(loadings1$ML1) >= 0.3 | abs(loadings1$ML2) >= 0.3 | abs(loadings1$ML3) >= 0.3) & sum(abs(loadings1$ML1) >= 0.3 , abs(loadings1$ML2) >= 0.3 , abs(loadings1$ML3) >= 0.3 ) == 1,])

It only returns an empty character and I know that it is not because there are no cases that match my criteria.它只返回一个空字符,我知道这不是因为没有符合我的标准的案例。

I also tried:我也试过:

row.names(loadings1[abs(loadings1$ML1) >= 0.3 | abs(loadings1$ML2) >= 0.3 | abs(loadings1$ML3) >= 0.3 & sum(abs(loadings1$ML1) >= 0.3 , abs(loadings1$ML2) >= 0.3 , abs(loadings1$ML3) >= 0.3 ) == 1,])

The second attempt seemed to ignore the & condition entirely even though the different or conditions worked.第二次尝试似乎完全忽略了 & 条件,即使不同的 or 条件有效。

I also found this for help and didn't really see why it wouldn't work in my case.我也找到了这个来寻求帮助,但并没有真正明白为什么它在我的情况下不起作用。

Any ideas?有任何想法吗?

You can use rowSums to find rows where exactly one variable is >= 0.3 .您可以使用rowSums来查找正好有一个变量>= 0.3

items <- row.names(loadings1)[rowSums(abs(loadings1[c("ML1","ML2","ML3")]) >= 0.3) == 1]
items
#[1] "6" "7"

Data:数据:

set.seed(1)
(loadings1 <- data.frame(id = 1:10, ML1 = rnorm(10), ML2 = rnorm(10), ML3 = rnorm(10)))
#   id        ML1         ML2         ML3
#1   1 -0.6264538  1.51178117  0.91897737
#2   2  0.1836433  0.38984324  0.78213630
#3   3 -0.8356286 -0.62124058  0.07456498
#4   4  1.5952808 -2.21469989 -1.98935170
#5   5  0.3295078  1.12493092  0.61982575
#6   6 -0.8204684 -0.04493361 -0.05612874
#7   7  0.4874291 -0.01619026 -0.15579551
#8   8  0.7383247  0.94383621 -1.47075238
#9   9  0.5757814  0.82122120 -0.47815006
#10 10 -0.3053884  0.59390132  0.41794156

Using a dplyr method, you can do it this way.使用dplyr方法,您可以这样做。 case_when is useful when applying multiple conditions. case_when在应用多个条件时很有用。

set.seed(1)
df <- tibble(id = 1:10, ML1 = rnorm(10), ML2 = rnorm(10), ML3 = rnorm(10))
df %>% 
  mutate(
    Which = case_when(
      abs(ML1) >= 0.3 & abs(ML2) < 0.3 & abs(ML3) < 0.3 ~ "ML1",
      abs(ML2) >= 0.3 & abs(ML1) < 0.3 & abs(ML3) < 0.3 ~ "ML2",
      abs(ML3) >= 0.3 & abs(ML1) < 0.3 & abs(ML2) < 0.3 ~ "ML3",
    )
  ) %>%
  filter(!is.na(Which)) %>%
  select(id) %>%
  unlist

This prints a list of the ids which qualify.这将打印符合条件的 id 列表。 You could also leave it as a data frame and get more information, such as which ML column was >= 0.3.您也可以将其保留为数据框并获取更多信息,例如哪个 ML 列 >= 0.3。

Edit: Forgot absolute value.编辑:忘记绝对值。 Also, Using your method would work, but you have to use parentheses around each condition such as (cond1 & !cond2 & !cond3) |此外,使用您的方法会起作用,但是您必须在每个条件周围使用括号,例如 (cond1 & !cond2 & !cond3) | (!cond1 & cond2 & !cond3). (!cond1 & cond2 & !cond3)。

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

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