简体   繁体   中英

R - setting NA to zero in an aggregate function

In R, I'm trying to run the following code to find the variance of several rows in a data frame. The variance is correctly calculated when there is more than 1 case, but obviously produces NA when there is only one case of ID1&ID2.

collated <- aggregate(.~ID1+ID2, interactionData, FUN=var, na.rm=TRUE) 

In the above code, is there a way to force R to set to 0 rather than NA

(I know that I could run a line of code afterwards along the lines of collated[is.na(collated)] <- 0 . However, I'm just wondering if there is a ay to do this all within the aggregate function.

We can create an if/else condition based on the length to output those with length as 1 to 0 or else get the var of the variable.

aggregate(.~ID1+ID2, interactionData, FUN=function(x)
         if(length(x)==1) 0 else var(x, na.rm=TRUE))

data

set.seed(24)
interactionData <- data.frame(ID1= rep(1:4, each=3), 
  ID2= sample(LETTERS[1:5], 12, replace=TRUE), value= rnorm(12))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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