Domanda

I am a beginner in R, but I am aware I should look for answers before asking a question here. I did, looked into help files, but to no avail. The problem is as follows: when I ask for a summary of subset X, the output of the two columns is as below. I wanted to have only the output for the answer, which I am able to to, but it is presented differently (see the output at the bottom). I want to have the results presented as a table, not as a list.

summary(X, max = 12)

results in:

student            answer    


 Min.   :    335   0 - Not at all likely                                                                 :  35  
 1st Qu.: 855480   1                                                                                     :  18  
 Median :1831962   10 - Extremely likely                                                                 :9336  
 Mean   :1519041   2                                                                                     :  23  
 3rd Qu.:2183663   3                                                                                     :  19  
 Max.   :2607132   4                                                                                     :  15  
                   5 - Neutral                                                                           : 939  
                   6                                                                                     : 235  
                   7                                                                                     : 921  
                   8                                                                                     :1844  
                   9                                                                                     :1194  
                   option_i4x-DelftX-ET3034TUx-problem-b3d30df864ca41ffa0170e790f01a783_2_1_dummy_default:  71

Because I am only interested in the summary stats for answer, I used

summary(X$answer, max = 12)

And then I get the list below as answer.

                                                             0 - Not at all likely 
                                                                                35 
                                                                                 1 
                                                                                18 
                                                             10 - Extremely likely 
                                                                              9336 
                                                                                 2 
                                                                                23 
                                                                                 3 
                                                                                19 
                                                                                 4 
                                                                                15 
                                                                       5 - Neutral 
                                                                               939 
                                                                                 6 
                                                                               235 
                                                                                 7 
                                                                               921 
                                                                                 8 
                                                                              1844 
                                                                                 9 
                                                                              1194 
option_i4x-DelftX-ET3034TUx-problem-b3d30df864ca41ffa0170e790f01a783_2_1_dummy_default 
                                                                                71 
È stato utile?

Soluzione

You should try

summary(X["answer"], max = 12)

since X["answer"] is not a vector like X$answer but a one-column data frame.

Altri suggerimenti

EDIT: I just found out that if you want to save/export, my solution below gives more useful output (as a table).

write.csv(data.frame(summary(X$answer)), "X.csv")

I played around a bit more, and with @JT85's suggestion, I found a nice solution.

data.frame(summary(X$answer))

and

data.frame(table(X$answer))

both work and give the output I want.

PS. It is a coincidence I found it so quickly after posting the question. This has been bugging me for 2 days already.

The output I get for data.frame(summary...) is as follows:

                                                                                      summary.A1.answer.
0 - Not at all likely                                                                                  35
1                                                                                                      18
10 - Extremely likely                                                                                9336
2                                                                                                      23
3                                                                                                      19
4                                                                                                      15
5 - Neutral                                                                                           939
6                                                                                                     235
7                                                                                                     921
8                                                                                                    1844
9                                                                                                    1194
option_i4x-DelftX-ET3034TUx-problem-b3d30df864ca41ffa0170e790f01a783_2_1_dummy_default                 71
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top