2017-02-15 2 views
2

Rのデフォルト[1]の代わりに、出力に "mean ="をどのように追加しますか?Rのデフォルト[1]ではなく、 "mean ="を出力にどのように追加しますか?

Test_scores <- c(50,75,80,90,99,93,65,85,95,87) #created matrix 
Hours_studied <- c(.1,.5,.6,1,3,3.5,.5,1,2,2.5) 
grade_study <- cbind(Test_scores,Hours_studied) #combined into one matrix 
return(grade_study) 

summarystat <- function(x) { #make a function to output the mean, median, sd with the output labeled (ex: mean=) 
    print(mean(x)), 
    print (median(x)) 
    print(sd(x)) 
} 

答えて

4

私はこのような何かをするだろう:

summarystat <- function(x) { 
    cat(sprintf("The mean is %s\n", mean(x))) 
    cat(sprintf("The median is %s\n", median(x))) 
    cat(sprintf("The sd is %s\n", sd(x))) 

} 

summarystat(grade_study) 

を出力は、次のとおりです。あなたが "="、その後に署名したい場合

The mean is 41.685 
The median is 26.75 
The sd is 42.5506419643576 

は、あなたができる:

summarystat <- function(x) { 
    cat(sprintf("mean = %s\n", mean(x))) 
    cat(sprintf("median = %s\n", median(x))) 
    cat(sprintf("sd = %s\n", sd(x))) 

} 

summarystat(grade_study) 

出力は

です。
mean = 41.685 
median = 26.75 
sd = 42.5506419643576 
関連する問題