简体   繁体   English

有条件地对 R 中的列求和

[英]Conditionally sum columns in R

I'm working on converting Stata code to R. There's a snippet of code that creates a new variable and adds the column value if it meets specific parameters.我正在将 Stata 代码转换为 R。有一段代码可以创建一个新变量并在满足特定参数时添加列值。 For example, if a cell is greater than 0 and less than or equal to 3, that value would be added to newvar例如,如果单元格大于 0 且小于或等于 3,则该值将添加到newvar

gen newvar=0
 
local list a b c
foreach x of local list{
    qui replace newvar=newvar+`x' if `x'>0 & `x'<=3 
}
set.seed(5)
dat <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))

在此处输入图像描述

Desired Output所需 Output

在此处输入图像描述

A tidyverse approach一种tidyverse的方法

library(dplyr)
set.seed(5)
dat <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))

conditional_sum <- function(x,a = 0,b = 3){
  sum(x[x > a & x <= b],na.rm = TRUE)
}

dat %>% 
  rowwise() %>% 
  mutate(newvar = conditional_sum(c_across()))

# A tibble: 5 x 4
# Rowwise: 
        a      b      c newvar
    <dbl>  <dbl>  <dbl>  <dbl>
1 -0.841  -0.603  1.23  1.23  
2  1.38   -0.472 -0.802 1.38  
3 -1.26   -0.635 -1.08  0     
4  0.0701 -0.286 -0.158 0.0701
5  1.71    0.138 -1.07  1.85 

Replace the elements that are not satisfying the condition to NA and get the rowSums on the rest of the elements to create the 'newvar'将不满足条件的元素替换为NA ,获取元素的rowSums上的rowSums,创建'newvar'

dat$newvar <-  rowSums(NA^(dat <=0|dat >=3)*dat, na.rm = TRUE)

-output -输出

> dat
            a          b          c     newvar
1 -0.84085548 -0.6029080  1.2276303 1.22763034
2  1.38435934 -0.4721664 -0.8017795 1.38435934
3 -1.25549186 -0.6353713 -1.0803926 0.00000000
4  0.07014277 -0.2857736 -0.1575344 0.07014277
5  1.71144087  0.1381082 -1.0717600 1.84954910

A common way to perform rowwise operations is using the apply function. Eg:执行按行操作的常用方法是使用apply function。例如:

dat$newvar <- apply(dat, 1, \(r) sum(r[r > 0 & r <= 3]))

Read as: Apply a function to every row of dat .读作:将 function 应用于dat的每一行。 The function takes a vector r , and sums the elements of r which satisfy the criterio. function 采用向量r ,并对满足条件的r的元素求和。

Results in结果是

            a          b          c     newvar
1 -0.84085548 -0.6029080  1.2276303 1.22763034
2  1.38435934 -0.4721664 -0.8017795 1.38435934
3 -1.25549186 -0.6353713 -1.0803926 0.00000000
4  0.07014277 -0.2857736 -0.1575344 0.07014277
5  1.71144087  0.1381082 -1.0717600 1.84954910

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

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