0

Can you please let me know how to calculate the mean of a column while ignoring the zeros in the column. I tried mean(df$Col1, na.rm=F) it did not work as this only ignores the NA's in the column.

Please let me know how if there is a way to perform this easily.

structure(list(`Row Labels` = c("2019-01-01", "2019-02-01", "2019-03-01", 
"2019-04-01", "2019-05-01", "2019-06-01", "2019-07-01", "2019-08-01", 
"2019-09-01", "2019-10-01", "2019-11-01", "2019-12-01", "2020-01-01", 
"2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01", "2020-06-01", 
"2020-07-01", "2020-08-01", "2020-09-01", "2020-10-01", "2020-11-01", 
"2020-12-01", "2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01", 
"2021-05-01", "2021-06-01", "2021-07-01", "2021-08-01", "2021-09-01", 
"2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-02-01", 
"2022-03-01", "2022-04-01", "2022-05-01", "2022-06-01", "2022-07-01", 
"2022-08-01", "2022-09-01", "2022-10-01"), `XYZ|574` = c(0, 0, 
0, 0, 0, 0, 0, 0, 74, 179, 464, 880, 324, 184, 90, 170, 140, 
96, 78, 83, 83, 121, 245, 740, 332, 123, 117, 138, 20, 42, 70, 
70, 42, 103, 490, 641, 488, 245, 142, 95, 63, 343, 57, 113, 100, 
105)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-46L))

The code I wrote was based on this, Calculate mean, median by excluding any given number but I couldn't find a solution using this:

library(readxl)
library(dplyr)

Book2 <- read_excel("X:/X/X/X- X/X/Book2.xlsx",sheet = "Sheet2")
mean(Book2$`XYZ|574`)
mean(Book2$`XYZ|574`[`XYZ|574`>0])

The first part of mean worked but the second part did not work. And I have to specify the column name as there are multiple columns in the real situation I am dealing with. The current mean including the 0's are 171.5217 But the expected mean after excluding the 0's is 207.63.

Please let me know how to solve this situation.

user20203146
  • 447
  • 7

0 Answers0