I have an if
loop over multiple data.frame
-s. I subset the data based on one of the variables and sum
the entries based on another variable. My problem is that the selection sometimes returns a zero element tibble
and so the sum
function throws an error. Is there an elegant way to make the sum
function execute these cases and consider the empty tibble
as zero? It works nicely with a data.frame
but does not with a tibble
. I am sure there should be a solution with tibble
as well.
#tibble
data<- tibble:: tibble("A" = c(1, 1, 2), "B"= c(2, 2, 1), "C"= c(10, 20, 30))
good<- data[data$A == 1,]
sum(good[good$B == "1",'C'])
#data.frame
data<- data.frame ("A" = c(1, 1, 2), "B"= c(2, 2, 1), "C"= c(10, 20, 30))
good<- data[data$A == 1,]
sum(good[good$B == "1",'C'])