No output when use "paste" keyword in a function

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

No output when use "paste" keyword in a function

WuyiL
This post has NOT been accepted by the mailing list yet.
Hi, I am new to R and right a simple function today. However, I couldn't get any output use my defined function. What is output function like "Printout" in R. Many thanks.

chem_levels is a dataset with 20 obs and 4 variables.

simplef<-function(x){
   for (i in 1:4){
     if (chem_levels[x,i]>mean(chem_levels[,i])){
        number = number + 1
        ID = c(ID,i)
        }
   }      
   if (number != 0){
     paste("you're above average on", number,"chemicals.")
     for (j in 1:length(ID)){paste(colnames(chem_levels)[j],"your level is",chem_levels[ID[j],x],".")}
   }
   if (number == 0) paste("Congrats, all of your chemical levels are below average.")
}


 
Reply | Threaded
Open this post in threaded view
|

Re: No output when use "paste" keyword in a function

WuyiL
This post has NOT been accepted by the mailing list yet.
I changed the keyword from "paste" to "print", but instead of return nothing, I got an error.

Error in print.default("you're above average on", number, "chemicals.") :
  invalid 'quote' argument

Could anyone help me?
Many thanks,
Reply | Threaded
Open this post in threaded view
|

Re: No output when use "paste" keyword in a function

WuyiL
This post has NOT been accepted by the mailing list yet.
In reply to this post by WuyiL
It seems that the problem is not come with the "print" and "paste".

The function work when chem_levels[x,i] < mean(chem_levels[,i]), number = 0

However, it comes with an error, chem_levels[x,i] > mean(chem_levels[,i])

For instance, when x=5, number should be 5

when I run simplef(5)

there was an error said:

Error in print.default(colnames(chem_levels)[j], "your level is", chem_levels[ID[j],  :
  invalid 'digits' argument

Also, I found the number and ID are still stay as initial.

Any help?


Reply | Threaded
Open this post in threaded view
|

Re: No output when use "paste" keyword in a function

Bart Joosen
This post has NOT been accepted by the mailing list yet.
In reply to this post by WuyiL
Hi,

paste combines the strings, print just prints them.
You should use: print(paste(....))

So:
simplef<-function(x){
   for (i in 1:4){
     if (chem_levels[x,i]>mean(chem_levels[,i])){
        number = number + 1
        ID = c(ID,i)
        }
   }      
   if (number != 0){
     print(paste("you're above average on", number,"chemicals."))
     for (j in 1:length(ID)){paste(colnames(chem_levels)[j],"your level is",chem_levels[ID[j],x],".")}
   }
   if (number == 0) paste("Congrats, all of your chemical levels are below average.")
}


HTH

Bart
Reply | Threaded
Open this post in threaded view
|

Re: No output when use "paste" keyword in a function

WuyiL
This post has NOT been accepted by the mailing list yet.
Thanks, it works perfectly.