Using paste0 within a function.


I am using the following to calculate mean and SD at the same time:

mean_sd <- function(msd)
return(c(mean(msd), sd(msd)))

[1] 20.090625 6.026948

The result is enough for me, but I am still curious why the following is not working. I am just trying to print 'mean=' and 'SD=' before the corresponding values.

mean_sd <- function(msd)
return(print(paste0(mean= c(mean(msd)), paste0(SD=sd(msd)))))

I am a new user of the paste function, so I am not sure if the code is wrong or if using paste is not permitted in this context.

Please let me know if you have any idea.

Thank you.

Since 'mean=' and 'SD=' are text, you simply need to wrap them in quotes in your function and add a comma after each.

mean_sd <- function(msd) {  
  return(print(paste0('mean=', c(mean(msd)), paste0('SD=', sd(msd)))))


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.