1

I was trying to add labels to a dataset. The labels are always ordered alphabetically, instead of by value. Please help me out. The codes are:

sat.df<- data.frame(sapply(satdata, factor,
                       levels = c( 1, 2, 3, 4, 5, 9),
                       labels = c( "Not at all Satisfied", "Not satisfied", "Neural",
                                   "Satisfied", "Very Satisfied", "Not Applicable")))

The summary results I got are:

                    q1                        Q6_a    
 Neural              :116   Neural              : 35  
 Not Applicable      : 25   Not Applicable      : 47  
 Not at all Satisfied: 13   Not at all Satisfied:  1  
 Not satisfied       : 32   Not satisfied       : 14  
 Satisfied           :325   Satisfied           :132  
 Very Satisfied      :399   Very Satisfied      :171  
 NA's                :  6   NA's                :516 

I wonder if there's a way to reorder them from 1 to 9. Thank you very much!

dorayin
  • 127
  • 1
  • 2
  • 10
  • 1
    How to access `satdata`? The question seem not to be fully reproducible yet... – Heikki Dec 01 '17 at 19:33
  • Please see [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). All variables used in the question should be defined. The `sapply()` is likely causing the problem. Maybe try `lapply`. – MrFlick Dec 01 '17 at 19:37
  • Sorry guys, I would attach commands to create sample data next time. Thank you for your responses! – dorayin Dec 01 '17 at 19:42

1 Answers1

2

sapply will simplify to a character matrix by default, losing the factor level order. You can set simplify = F or use lapply instead.

# sample data (please include something like this
# in your question next time)
satdata = data.frame(q1 = 1:3, q2 = 3:5)

summary(data.frame(sapply(satdata, factor,
    levels = c( 1, 2, 3, 4, 5, 9),
    labels = c( "Not at all Satisfied", "Not satisfied", "Neural",
                "Satisfied", "Very Satisfied", "Not Applicable"),   
    simplify = FALSE)))

summary(data.frame(lapply(satdata, factor,
    levels = c( 1, 2, 3, 4, 5, 9),
    labels = c( "Not at all Satisfied", "Not satisfied", "Neural",
                "Satisfied", "Very Satisfied", "Not Applicable"))))

                    q1                       q2   
 Not at all Satisfied:1   Not at all Satisfied:0  
 Not satisfied       :1   Not satisfied       :0  
 Neural              :1   Neural              :1  
 Satisfied           :0   Satisfied           :1  
 Very Satisfied      :0   Very Satisfied      :1  
 Not Applicable      :0   Not Applicable      :0  
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294