有条件选择两个数据帧R

问题描述 投票:0回答:1

假设AB是两个dataframes,最快的方法是:

找到A>0andB<0的实例?即A大于零,前提是B小于零。

[两个dataframes具有相等的尺寸,非常大(>70GB),并且对column1中的Acoulmn1中的B等进行了操作。

A=c(-31.621, -40.149, -21.519, -33.773, -34.023, -38.345, -39.238, 
    -30.221, -22.278, 27.055)

B=c(-29.321, -26.398, -25.663, -26.423, -23.721, -17.418, -15.979, 
    -21.927, -16.398, -21.084)
r dataframe bigdata conditional-statements
1个回答
0
投票

这是您要寻找的吗?

library(tibble)
library(dplyr)

A <- c(-31.621, -40.149, -21.519, -33.773, -34.023, -38.345, -39.238, 
       -30.221, -22.278, 27.055) %>% 
  enframe(name = "ID")

B <- c(-29.321, -26.398, -25.663, -26.423, -23.721, -17.418, -15.979, 
       -21.927, -16.398, -21.084) %>% 
  enframe(name = "ID")

A %>% 
  left_join(., B, by = "ID", suffix = c("_A", "_B")) %>% 
  mutate(condition = case_when(value_A > 0 & value_B < 0 ~ T,
                               TRUE ~ F))

#> # A tibble: 10 x 4
#>       ID value_A value_B condition
#>    <int>   <dbl>   <dbl> <lgl>    
#>  1     1   -31.6   -29.3 FALSE    
#>  2     2   -40.1   -26.4 FALSE    
#>  3     3   -21.5   -25.7 FALSE    
#>  4     4   -33.8   -26.4 FALSE    
#>  5     5   -34.0   -23.7 FALSE    
#>  6     6   -38.3   -17.4 FALSE    
#>  7     7   -39.2   -16.0 FALSE    
#>  8     8   -30.2   -21.9 FALSE    
#>  9     9   -22.3   -16.4 FALSE    
#> 10    10    27.1   -21.1 TRUE

reprex package(v0.3.0)在2020-02-25创建

© www.soinside.com 2019 - 2024. All rights reserved.