-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]])

ash7on
  • 39
  • 5
  • Welcome! Please make your question reproducible: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – broti Apr 20 '20 at 15:13
  • i mean it is reproducible as that line of code will be universally the same. – ash7on Apr 20 '20 at 15:15
  • I guess what @broti means is.. the code you have above, setNames(..) we can run it without it throwing an error. The syntax is wrong. Can you elaborate on what you need using the iris example, and what might be your expected output? – StupidWolf Apr 20 '20 at 15:21
  • ill try with the iris example in a minute, the out put in my actual data set is a long list of medians for all my row names, these are the medians of multiple columns that are the results from repetitions of a test. many of the medians produced are 0 while many others are positive integers. I was wondering if there was a line of code that could be added to mean only answers above 0 are shown – ash7on Apr 20 '20 at 15:32
  • i was thinking possibly could done with a ```0 <``` somewhere but unsure – ash7on Apr 20 '20 at 15:33

1 Answers1

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

  • Matrix/Vector/Dataframe: In this example the df containing the iris dataset
  • Margin: In this example, margin is set to 1, meaning the apply will iterate through each row. To iterate over each column, change the 1 to a 2, or for every cell, change the 1 to c(1,2)
  • Function to apply over margin: In this case we are creating a custom function and passing each row into this custom function under the variable x. We will then remove all non zero values from the row and finally return the median

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

Jamie_B
  • 299
  • 1
  • 5