0
votes

I have some data, let's say the mtcars dataset in R , and I want to export the describe function output to a csv file in a good format.

For the describe function output , we can use:

install.packages(Hmisc)  
library(Hmisc)
mt_data = mtcars
x = describe(mt_data)

Tried to write to CSV file1:

write.csv(x, file="names22.csv",row.names = F)    

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""describe"" to a data.frame

Attempt2

data = mtcars  
x = describe(data)
write.csv(t(as.matrix(x)), file="name2.csv") 

But the above command is giving the output in a very weird format.

Expected output

        x1   x2  x3  x4 
mpg     12   23  758 23
cyl     2    23  23  64
disp    2    31  32  53
xyz     4    21  32  23
skjf    8    21  23  23

The above format is what I'm trying to achieve to write in a csv file.

1
Help to close the below one as recommended answer - if it solves your problemPrasanna Nandakumar

1 Answers

1
votes
> library(psych)    
> mt_dat <- describeBy(mt_data)
> mt_dat
     vars  n   mean     sd median trimmed    mad   min    max  range  skew kurtosis    se
mpg     1 32  20.09   6.03  19.20   19.70   5.41 10.40  33.90  23.50  0.61    -0.37  1.07
cyl     2 32   6.19   1.79   6.00    6.23   2.97  4.00   8.00   4.00 -0.17    -1.76  0.32
disp    3 32 230.72 123.94 196.30  222.52 140.48 71.10 472.00 400.90  0.38    -1.21 21.91
hp      4 32 146.69  68.56 123.00  141.19  77.10 52.00 335.00 283.00  0.73    -0.14 12.12
drat    5 32   3.60   0.53   3.70    3.58   0.70  2.76   4.93   2.17  0.27    -0.71  0.09
wt      6 32   3.22   0.98   3.33    3.15   0.77  1.51   5.42   3.91  0.42    -0.02  0.17
qsec    7 32  17.85   1.79  17.71   17.83   1.42 14.50  22.90   8.40  0.37     0.34  0.32
vs      8 32   0.44   0.50   0.00    0.42   0.00  0.00   1.00   1.00  0.24    -2.00  0.09
am      9 32   0.41   0.50   0.00    0.38   0.00  0.00   1.00   1.00  0.36    -1.92  0.09
gear   10 32   3.69   0.74   4.00    3.62   1.48  3.00   5.00   2.00  0.53    -1.07  0.13
carb   11 32   2.81   1.62   2.00    2.65   1.48  1.00   8.00   7.00  1.05     1.26  0.29