Export describe.by (package psych) as csv file in R
Asked Answered
W

2

8

Does anyone know how to export describe.by statistics to csv in R? I get this message:

estatistica <- describe.by(pag,list(pag$Jogo)

  write.table(estatistica,file="H:/Myfile.csv",sep=",")
  "Erro em as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""by"" to a data.frame"

Since its such a general question, a general example would do.

        Jogo  Pais  Numero
          A  Canada    1
          B  Canada    2
          C  Canada    1
          D  Canada    4
          A  Brazyl    6
          B  Brazyl    7
          A  France    1
          B  France    1
          C  France    2
          D  France    3
Wexler answered 29/4, 2013 at 13:18 Comment(2)
He. Welcome to SO. It's the general norm to provide a reproducible example for those trying to help. That means we can run your example precisely. Also please use code tags to indicate code. Use ` marks for in text and the {} button to show code blocks.Kilowatt
I thought about giving a example, but its such a general question, I didnt thought I had to. But I can do any example.Wexler
S
9

Function describeBy() (this should be used instead of descrive.by() that is deprecated) produces list of data frame and therefore can't be written to file with write.table().

library(psych)    
estatistica <- describeBy(pag,list(pag$Jogo))
estatistica

: A
       var n mean   sd median trimmed  mad min max range skew kurtosis   se
Jogo*    1 3 1.00 0.00      1    1.00 0.00   1   1     0  NaN      NaN 0.00
Pais*    2 3 2.00 1.00      2    2.00 1.48   1   3     2 0.00    -2.33 0.58
Numero   3 3 2.67 2.89      1    2.67 0.00   1   6     5 0.38    -2.33 1.67
-------------------------------------------------------------------- 
: B
       var n mean   sd median trimmed  mad min max range skew kurtosis   se
Jogo*    1 3 2.00 0.00      2    2.00 0.00   2   2     0  NaN      NaN 0.00
Pais*    2 3 2.00 1.00      2    2.00 1.48   1   3     2 0.00    -2.33 0.58
Numero   3 3 3.33 3.21      2    3.33 1.48   1   7     6 0.34    -2.33 1.86

To solve this problem one way would be to put all list elements into one data frame with do.call() and rbind() and then write to file. This will make data frame where group names will be added before original variable names.

estatistica2<-do.call("rbind",estatistica)
estatistica2

         var n mean   sd median trimmed  mad min max range skew kurtosis   se
A.Jogo*    1 3 1.00 0.00    1.0    1.00 0.00   1   1     0  NaN      NaN 0.00
A.Pais*    2 3 2.00 1.00    2.0    2.00 1.48   1   3     2 0.00    -2.33 0.58
A.Numero   3 3 2.67 2.89    1.0    2.67 0.00   1   6     5 0.38    -2.33 1.67
B.Jogo*    1 3 2.00 0.00    2.0    2.00 0.00   2   2     0  NaN      NaN 0.00
B.Pais*    2 3 2.00 1.00    2.0    2.00 1.48   1   3     2 0.00    -2.33 0.58
B.Numero   3 3 3.33 3.21    2.0    3.33 1.48   1   7     6 0.34    -2.33 1.86
C.Jogo*    1 2 3.00 0.00    3.0    3.00 0.00   3   3     0  NaN      NaN 0.00
C.Pais*    2 2 2.50 0.71    2.5    2.50 0.74   2   3     1 0.00    -2.75 0.50
C.Numero   3 2 1.50 0.71    1.5    1.50 0.74   1   2     1 0.00    -2.75 0.50
D.Jogo*    1 2 4.00 0.00    4.0    4.00 0.00   4   4     0  NaN      NaN 0.00
D.Pais*    2 2 2.50 0.71    2.5    2.50 0.74   2   3     1 0.00    -2.75 0.50
D.Numero   3 2 3.50 0.71    3.5    3.50 0.74   3   4     1 0.00    -2.75 0.50
Spunky answered 29/4, 2013 at 14:18 Comment(2)
Horray for the old and venerable do.call(rbind, ...) strategy.Japonica
If you use the matrix option of describeBy, you will get a data frame that can be written with the write.table command.Persnickety
P
8

Actually, there is an option in describeBy that creates matrix form output to just this. Using the original data,

describeBy(pag,pag$Jogo,mat=TRUE)

        item group1 var n     mean        sd median  trimmed    mad min max range      skew  kurtosis        se
Jogo*1     1      A   1 3 1.000000 0.0000000    1.0 1.000000 0.0000   1   1     0          NaN       NaN 0.0000000
Jogo*2     2      B   1 3 2.000000 0.0000000    2.0 2.000000 0.0000   2   2     0       NaN       NaN 0.0000000
Jogo*3     3      C   1 2 3.000000 0.0000000    3.0 3.000000 0.0000   3   3     0       NaN       NaN 0.0000000
Jogo*4     4      D   1 2 4.000000 0.0000000    4.0 4.000000 0.0000   4   4     0       NaN       NaN 0.0000000
Pais*1     5      A   2 3 2.000000 1.0000000    2.0 2.000000 1.4826   1   3     2 0.0000000 -2.333333 0.5773503
Pais*2     6      B   2 3 2.000000 1.0000000    2.0 2.000000 1.4826   1   3     2 0.0000000 -2.333333 0.5773503
Pais*3     7      C   2 2 2.500000 0.7071068    2.5 2.500000 0.7413   2   3     1 0.0000000 -2.750000 0.5000000
Pais*4     8      D   2 2 2.500000 0.7071068    2.5 2.500000 0.7413   2   3     1 0.0000000 -2.750000 0.5000000
Numero1    9      A   3 3 2.666667 2.8867513    1.0 2.666667 0.0000   1   6     5 0.3849002 -2.333333 1.6666667
Numero2   10      B   3 3 3.333333 3.2145503    2.0 3.333333 1.4826   1   7     6 0.3434206 -2.333333 1.8559215
Numero3   11      C   3 2 1.500000 0.7071068    1.5 1.500000 0.7413   1   2     1 0.0000000 -2.750000 0.5000000
Numero4   12      D   3 2 3.500000 0.7071068    3.5 3.500000 0.7413   3   4     1 0.0000000 -2.750000 0.5000000

Unfortunately, the current version does not have the rounded output that describeBy does normally. I have fixed this in Version 1.3.6 (about to be released).

Bill

Persnickety answered 9/6, 2013 at 17:56 Comment(4)
Well, it wasn't version 1.3.6, but 1.3.10 has this feature. (The digits option in describeBy)Persnickety
This is a bit old but can't quite find a better place to ask this - getting a "subscript out of bounds" error when grouping by two variables and mat=TRUE. (Works ok if mat=TRUE is removed.) Using psych version 1.8.12. Error in [<-(*tmp*, var, group + 1, value = dim.names[[group]][[groupi]]) : subscript out of boundsGuam
Can you post a minimal working example of this problem (perhaps using dput() for your data. I can not reproduce this error. I just tried des <- describeBy(sat.act,group=c("gender","education"),mat=TRUE) and it works. 'Persnickety
@WilliamRevelle when I convert it to a matrix, it pairs the variables together, as opposed to separating one group of variables from another. Is there a way to correct this?Gildus

© 2022 - 2024 — McMap. All rights reserved.