I have written the following function:
asteriks = function(pvalue){
if(pvalue > 0.05){
output = "NS"
}else if (pvalue <=0.05 & pvalue >0.01){
output = "*"
}else if (pvalue <=0.01 & pvalue >0.001){
output = "**"
}else if (pvalue <=0.001 & pvalue >0.0001){
output = "***"
}else if (pvalue <=0.0001){
output = "****"
}
return(output)
}
It works fine when I provide an argument of length 1, but I would like the function to take a vector of length >1 as an input and return a vector of the same length.
example of what I would like to do:
vector_pvals = c(0.1, 0.05, 0.001, 0.0001)
asteriks(vector_pvals)
The output should be a character vector like this:
[1] "NS" "*" "***" "****"
I can achieved this by using the function in a for
-loop of course, but I actually want to use it within a dplyr
pipe, so it would be nice to be able to just feed it a whole vector. Is the answer to use a for
-loop within the function to work one each element one at a time, or is there an easier way?