根据 R 中另一个变量的条件计算负值的数量

Count number of negative values based on a condition of another variable in R

  dummy value
1  TRUE  -2.0
2 FALSE   3.0
3 FALSE   4.0
4 FALSE   2.0
5  TRUE  -4.5
6  TRUE   9.0
7  TRUE  -1.0
8 FALSE   0.0
9  TRUE   3.0

structure(list(dummy = c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
TRUE, FALSE, TRUE), value = c(-2, 3, 4, 2, -4.5, 9, -1, 0, 3)), class = "data.frame", row.names = c(NA, 
-9L))

我想计算“值”列的负(和正)值的数量,以便进行观察,使得 dummy == TRUE。

任何帮助将不胜感激!

基础 R

使用by

by(df$dummy, df$value > 0, FUN = sum)

# df$value > 0: FALSE
# [1] 3
# --------------------------------------------------------------------- 
# df$value > 0: TRUE
# [1] 2

使用aggregate:

aggregate(dummy ~ value > 0, data = df, sum)

#   value > 0 dummy
# 1     FALSE     3
# 2      TRUE     2

dplyr

library(dplyr)
df %>% 
  group_by(sign = value > 0) %>% 
  summarise(sum = sum(dummy))

# A tibble: 2 x 2
#   sign    sum
#   <lgl> <int>
# 1 FALSE     3
# 2 TRUE      2

或者,使用 sign:

df %>% 
  group_by(sign = sign(value)) %>% 
  summarise(sum = sum(dummy))

# A tibble: 3 x 2
   sign   sum
  <dbl> <int>
1    -1     3
2     0     0
3     1     2

df <- structure(list(dummy = c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
TRUE, FALSE, TRUE), value = c(-2, 3, 4, 2, -4.5, 9, -1, 0, 3)), class = "data.frame", row.names = c(NA, 
-9L))

不太清楚您的预期输出应该是什么样子,dplyr 的一个选项可能是...

library(dplyr)

dat <- structure(list(dummy = c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
                         TRUE, FALSE, TRUE), value = c(-2, 3, 4, 2, -4.5, 9, -1, 0, 3)), class = "data.frame", row.names = c(NA, 
                                                                                                                             -9L))

dat %>% 
  filter(dummy) %>%
  mutate(pn = ifelse(value > 0, "pos", "neg")) %>% 
  group_by(pn) %>% 
  summarise(count = n())
#> # A tibble: 2 x 2
#>   pn    count
#>   <chr> <int>
#> 1 neg       3
#> 2 pos       2

reprex package (v2.0.1)

于 2022-02-18 创建