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. For example, if a cell is greater than 0 and less than or equal to 3, that value would be added to 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

