numerical data required

Hi ! I have this data, but when I want to calculate the median of "Score.on.Alumni" or any other column it says to me: "Error in median.default(rows\$Score.on.Alumni): numerical data needed
Thank you in advance!

The data type of those columns must be non-numeric. Try `median(as.numeric(rangs\$Total.Score))`.

Thanks for the reply !
Here, in my answer sheet we used median(rangs\$Score.on.Alumni) and they get "28.8". (with my console I get an error)
When I tried with yours median(as.numeric((rangs\$Score.on.Alumni)), I got "15.5".
Do you know why ?

Hard to say without seeing the data. Can you show us the complete contents of the vector `rangs\$Score.on.Alumni`?

Also, please familiarize yourself with our homework policy since this appears to be an assignment.

Please do not post screenshots. It's much easier if you generate the code required to reproduce the data. For example, try running `dput(rangs\$Score.on.Alumni))` and copying the output here.

Also, as I mentioned before, the data type of your column is definitely not numeric even though it appears to contain numbers.

Sorry I deleted my screenshots, I get this exactly
structure(c(2L, 25L, 33L, 36L, 34L, 29L, 35L, 31L, 32L, 30L,
28L, 26L, 13L, 6L, 17L, 15L, 24L, 1L, 27L, 18L, 24L, 20L, 7L,
14L, 16L, 23L, 21L, 11L, 8L, 19L, 9L, 7L, 18L, 5L, 1L, 7L, 12L,
8L, 22L, 10L, 7L, 16L, 3L, 1L, 1L, 16L, 4L, 13L, 11L, 1L), .Label = c("0",
"100", "13,2", "14,4", "15,6", "16,6", "19,5", "20,4", "21,2",
"22,8", "23,5", "24,3", "25,6", "26,3", "27", "28,8", "33,3",
"33,8", "35,8", "37,2", "37,7", "38,4", "39", "40,3", "42", "43,6",
"48,1", "50,9", "55,5", "60,3", "62,3", "70,8", "72,5", "74,6",
"76", "93,6"), class = "factor")

EDIT: My explanation here is incorrect. Please see andresrcs' post below.

Thank you. As you can see from the output, the variable's data type is `factor`. You can cast it to `numeric` and make the change permanent as follows:

``````rangs\$Score.on.Alumni <- as.numeric(rangs\$Score.on.Alumni)
``````

As for why your answer sheet gives a value of 28.8 for the median, I cannot say. The median value of this set of numbers is 15.5.

Actually, using `as.numeric()` on a factor variable would give you the value of the levels, not the actual numbers, there are many ways of solving this but I like to use `readr::parse_number()`

``````library(readr)

rangs <- data.frame(
Score.on.Alumni = as.factor(c("100","42",
"72,5","93,6","74,6","55,5","76","62,3",
"70,8","60,3","50,9","43,6","25,6","16,6",
"33,3","27","40,3","0","48,1","33,8","40,3",
"37,2","19,5","26,3","28,8","39","37,7",
"23,5","20,4","35,8","21,2","19,5","33,8",
"15,6","0","19,5","24,3","20,4","38,4",
"22,8","19,5","28,8","13,2","0","0","28,8",
"14,4","25,6","23,5","0"))
)

median(parse_number(as.character(rangs\$Score.on.Alumni),
locale = locale(decimal_mark = ",")))
#> [1] 28.8
``````

Created on 2020-04-26 by the reprex package (v0.3.0)

1 Like

Ah, I hadn't realized that! Thank you for pointing it out.

Thanks all for the reply ! It's helped me a lot.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.