# [R] Weird problem with median on a factor

Christoph Bier christoph.bier at web.de
Sun Nov 2 16:53:39 CET 2003

```Peter Dalgaard wrote:
> Christoph Bier <christoph.bier at web.de> writes:
>
>
>> Dave Cacela schrieb:
>>
>>> Christoph, I concur with the other respondents who questioned why someone
>>> would wish to calculate the median of a factor. However, with regard to
>>> your actual question, I suspect that median() is giving different answers
>>> because the two vectors are not both factors, i.e., that one of them is a
>>> character. Did you test that?
>>
>> Yes, I did and find the same like Tony Plate:
>>
>>> is.factor(fbhint.spss1\$V15.SPS) # = column 264
>> [1] TRUE
>>> mode(fbhint.spss1\$V15.SPS)
>> [1] "numeric"
>>> is.factor(fbhint.spss1\$V15.SP1) # = column 566
>> [1] TRUE
>>> mode(fbhint.spss1\$V15.SP1)
>> [1] "numeric"
>>
>>
>>> Using S, I have seen quirks in this regard that relate to import
>>> procedure and the value of the first element in the vector. In your case,
>>> the first elements differ in that one is NA while the other is
>>> "teils/teils".
>>
>> It also occurs if the first element is the same. For example "wichtig" in
>> columns 263 and 565.
>
>
> I suspect the solution to the riddle was given earlier (I forget by

Yes, you're right. I just wanted to answer Dave's question.
Nevertheless I don't understand what I observed above, maybe because of a
lack of knowledge/understanding of R. But I'm reading your and Brian Ripley's
book ...

[...]

Best regards,

Christoph

```