Search code examples
rdataframelogarithmrowsum

Exclude values within a range for rowsum in R


I'm trying to get the row sums for each row of my dataframe but with a condition. I'd like to exclude all the values that are between -1 and 1 after applying log2. I know how to exclude NAs but I'm confused with excluding actual numbers. My dataframe is just numbers, except for the row and column names.


Solution

  • Try

     dat1 <- dat
     dat1[dat1 >-1  & dat1<1] <- NA
     rowSums(dat1, na.rm=TRUE)
    

    If there are no NAs in the dataset, you could assign the values to 0 and just use rowSums

     dat1[dat1 >-1  & dat1<1] <- 0
     rowSums(dat1)
    

    data

     set.seed(42)
     dat <- as.data.frame(matrix(sample(seq(-5,5,by=0.25), 20*5,
                               replace=TRUE), ncol=5))