cmirian
cmirian

Reputation: 2253

How to remove automated text in function?

I am new to functions, and trying to learn

Let us say that I have

norm <- function(z){

  n = as.numeric(table(is.na(z))[1])
  m = mean(z, na.rm=T)
  stddev = sd(z, na.rm=T)
  
outdata = data.frame(n=n, 
                     mean=round(m, digits = 1), 
                     sd=round(stddev, digits = 1),
                     median=round(median(z), digits = 1),
                     Q1=round(quantile(z)[2], digits = 1),
                     Q3=round(quantile(z)[4], digits = 1),
                     lower_t.dist=round(m-sqrt(1+(1/n)) * qt(0.975, df=n-1) * stddev, 
                                        digits = 1), 
                     upper_t.dist=round(m+sqrt(1+(1/n)) * qt(0.975, df=n-1) * stddev, 
                                        digits = 1),
                     lower_frac = round(quantile(z, probs = c(0.025)), 
                                        digits = 1),
                     upper_frac = round(quantile(z, probs = c(0.975)),
                                        digits = 1))
return(outdata)
}

And then type norm(iris$Sepal.Length)

Which gives

> norm(iris$Sepal.Length)
      n mean  sd median  Q1  Q3 lower_t.dist upper_t.dist lower_frac upper_frac
25% 150  5.8 0.8    5.8 5.1 6.4          4.2          7.5        4.5        7.7

Question: why does it automatically print the 25% on the left side, and how to remove it so its just a blank space?

Upvotes: 0

Views: 33

Answers (1)

Taufi
Taufi

Reputation: 1577

If you don't want to see any rownames, not even numbering, you can do

norm <- function(z){
  
  n = as.numeric(table(is.na(z))[1])
  m = mean(z, na.rm=T)
  stddev = sd(z, na.rm=T)
  
  outdata = data.frame(n=n, 
                       mean=round(m, digits = 1), 
                       sd=round(stddev, digits = 1),
                       median=round(median(z), digits = 1),
                       Q1=round(quantile(z)[2], digits = 1),
                       Q3=round(quantile(z)[4], digits = 1),
                       lower_t.dist=round(m-sqrt(1+(1/n)) * qt(0.975, df=n-1) * stddev, 
                                          digits = 1), 
                       upper_t.dist=round(m+sqrt(1+(1/n)) * qt(0.975, df=n-1) * stddev, 
                                          digits = 1),
                       lower_frac = round(quantile(z, probs = c(0.025)), 
                                          digits = 1),
                       upper_frac = round(quantile(z, probs = c(0.975)),
                                          digits = 1))
             rownames(outdata) <- rep("", nrow(outdata))
  return(outdata)
}

and then you get

> norm(iris$Sepal.Length)
   n mean  sd median  Q1  Q3 lower_t.dist upper_t.dist lower_frac upper_frac
 150  5.8 0.8    5.8 5.1 6.4          4.2          7.5        4.5        7.7

Upvotes: 1

Related Questions