in a function for mean or medians can a line of code be added the will get rid of those = 0
example of function: df <- iris
setNames(apply(sepal.length, 1, median, na.rm = TRUE), df[[1]])
in a function for mean or medians can a line of code be added the will get rid of those = 0
example of function: df <- iris
setNames(apply(sepal.length, 1, median, na.rm = TRUE), df[[1]])
The following will calculate the median for all non-zero and non na values in each row of a data frame.
df <- iris
apply(df, 1, FUN = function(x){
values_to_calculate <- x[which(!x == 0)]
return(median(values_to_calculate, na.rm = T))
})
The apply function takes three parameters, view the documentation here: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/apply
Note: I would generally not recommend removing 0 from the median calculation unless the reason why the zero's are in the dataset in the first place is well known