[R] wilcox.test; data type conversion?

Peter Dalgaard pdalgd at gmail.com
Sat Oct 30 16:47:52 CEST 2010


On 10/29/2010 05:41 AM, Dennis Murphy wrote:
> Hi:
> 
> Make grade an ordered factor:
> grade <- factor(grade, levels = c('G', 'VG', 'MVG'))
> as.numeric(as.character(grade)) will convert to numeric scores 1, 2 and 3,
> respectively, corresponding to the numerical codes of the ordered levels.

Not quite:

> g <-  c('G', 'VG', 'MVG')
> grade <- factor(g, levels = c('G', 'VG', 'MVG'))
> as.numeric(as.character(grade))
[1] NA NA NA

However:

> as.numeric(grade)
[1] 1 2 3

(also, that's not an ordered factor, but it doesn't matter, the levels
have an order anyway)

> 
> HTH,
> Dennis
> 
> On Thu, Oct 28, 2010 at 8:37 PM, Par Leijonhufvud
> <par at hunter-gatherer.org>wrote:
> 
>> I'm working on a quick tutorial for my students, and was planning on
>> using Mann-Whitney U as one of the tests.
>>
>> I have the following (fake) data
>>
>>  grade <- c("MVG", "VG", "VG", "G", "MVG", "G", "VG", "G", "VG")
>>  sex <- c( "male", "male", "female", "male", "female", "male", "female",
>> "male", "male")
>>  gradesbysex <- data.frame(grade, sex)
>>
>> The grades is in the Swedish system, where the order is G < VG < MVG
>>
>> The idea is that they will investigate if they can show a grade
>> difference by sex (i.e. that the teacher gives better grades to boys or
>> girls).
>>
>> Since the wilcox.test needs the order of the grades it wants numeric
>> vector  for the data. Is there a good and simple (i.e. student
>> compatible) way to handle this? I could tell them to enter data as
>> numbers instead, but an elegant way to do this inside R would be
>> preferable.
>>
>>
>> On the same theme, is there a way to tell barplot that, when making
>> stacked barplots, to stack the data in a particular order (default
>> appears to be alphabetical)?

Presumably, same thing: Ensure that the stacking factor is a factor with
the levels in the correct order. However, you (Par) didn't tell how the
barplot was generated. The argument to the default method is a matrix
and the row/col ordering determines the plot. If, in turn, it was
created by tabulating two factors, then factor level ordering (by
default alphabetic) applies to the table.


-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list