0
x=iris[,1:4]

xx=matrix(ncol=4,nrow=6)


i=1
while(i<=4){  

  xx[1,i]=paste0("Min.  :",min(x[,i]))
  xx[2,i]=paste0("1st Qu.:",quantile(x[,i])[2])
  xx[3,i]=paste0("Median :",median(x[,i]))
  xx[4,i]=paste0("Mean  :",mean(x[,i]))
  xx[5,i]=paste0("3rd Qu.:",quantile(x[,i])[4])
  xx[6,i]=paste0("Max.  :",max(x[,i]))
  i=i+1
  dimnames(xx)=list(c("Min", "1st Qu", "median","mean","3rd Qu","Max"), names(x))
  dimnames(xx)[[1]]=rep("",nrow(xx))
  options=(digits=4)
}
print(xx)

This result is

Sepal.Length              Sepal.Width               Petal.Length   Petal.Width              
 "Min.  :4.3"              "Min.  :2"                "Min.  :1"     "Min.  :0.1"             
 "1st Qu.:5.1"             "1st Qu.:2.8"             "1st Qu.:1.6"  "1st Qu.:0.3"            
 "Median :5.8"             "Median :3"               "Median :4.35" "Median :1.3"            
 "Mean  :5.84333333333333" "Mean  :3.05733333333333" "Mean  :3.758" "Mean  :1.19933333333333"
 "3rd Qu.:6.4"             "3rd Qu.:3.3"             "3rd Qu.:5.1"  "3rd Qu.:1.8"            
 "Max.  :7.9"              "Max.  :4.4"              "Max.  :6.9"   "Max.  :2.5"

but i want to make

 Sepal.Length              Sepal.Width         Petal.Length        Petal.Width              
 Min.  :4.300              Min.   :2.000      Min.   :1.000        Min.   :0.100             
 1st Qu.:5.100             1st Qu.:2.800      1st Qu.:1.600        1st Qu.:0.300            
 Median :5.800             Median :3.000      Median :4.350        Median :1.300            
 Mean  :5.843              Mean   :3.057      Mean   :3.758        Mean   :1.199
 3rd Qu.:6.400             3rd Qu.:3.300      3rd Qu.:5.100        3rd Qu.:1.800            
 Max.  :7.900              Max.   :4.400      Max.   :6.900        Max.   :2.500

like this.

How can i make this table?

  1. without ""
  2. not 5.843333333 but 5.843

It's too difficult. How can i convert 5.84333 to 5.843?

showdev
  • 28,454
  • 37
  • 55
  • 73

4 Answers4

2

by the way why you make code like that? (just wonder) if you want to get those information you can use command summary

summary(iris[,1:4])
rischan
  • 1,553
  • 13
  • 19
1

You can round the values in your while loop, for example:

xx[4,i]=paste0("Mean  :", round(mean(x[,i]), digits = 3))
talat
  • 68,970
  • 21
  • 126
  • 157
  • @user3686685 Then use `xx[1,i]=paste0("Min. :",sprintf("%.3f",round(min(x[,i]), digits=3)))` – talat May 29 '14 at 13:46
0

Try

format(round(x, 3), nsmall = 3)

Related question is here: Formatting Decimal places in R

Community
  • 1
  • 1
Revive
  • 2,248
  • 1
  • 16
  • 23
0

The solution to your first question could be:

data.frame(xx)

your quotes is becouse of class matrix of you xx or print it without quotes:

print (xx, quote=F)
Andriy T.
  • 2,020
  • 12
  • 23