0

I have the following dataframe.

age      pclass         title
 32        2           Married Man
           3           Married Man
 40        1           Married Women
 47         3          Married Women
           2           Married Women
 5         3           Married Man

As you can see "AGE" column can have Missing Values, and while "pclass" column is a integer one, "title" column is a non integer column.

What I have to do is to replace Non Values from Age column with the median of the two other columns (title and pclass).

Replace non values from Age column by the median of pclass and title

I don't know if it's possible to calculate the median of two values.

Here is my code, it works but I don't know if it's correct.

median(as.numeric(titanic$title),titanic$pclass, na.rm = T)

Thanks.

Sotos
  • 44,023
  • 5
  • 28
  • 55
  • 1
    `as.numeric(string)` is always `NA`. So your median will always be the `pclass`...unless the `title` is a factor and you are just using its levels... but most importantly... Why do you want to do that? It makes no sense whatsoever. – Sotos Feb 09 '18 at 07:58
  • Is a problem from a Practice I have to do, its explication is the following one: Complete the missing values ​​now but with the median of the values ​​according to the variables pclass and title. – Sergio Urrea González Feb 09 '18 at 08:02
  • 1
    Oh good. Because age 4 and title Married Woman is quite worrying :/ – Sotos Feb 09 '18 at 08:04
  • it was an example that I put without thinking hahaha – Sergio Urrea González Feb 09 '18 at 08:05
  • Please make your example [reproducible](http://stackoverflow.com/questions/5963269) – Sotos Feb 09 '18 at 08:06
  • 1
    then I guess you're just supposed to take the **median of Age by pclass and title**... – Cath Feb 09 '18 at 08:06
  • This example is reproducible, there is no difference between age 4 and 47, the problem I have is, if it's possible to calculate the median as the explication I added in my previous post. – Sergio Urrea González Feb 09 '18 at 08:08
  • 1
    @Cath Replace the median of the values ​​according to the variables pclass and title – Sergio Urrea González Feb 09 '18 at 08:09
  • @Sotos ? tell me an exact duplicate question, he is calculating ALL the data at the same time(by group) not my question. – Sergio Urrea González Feb 09 '18 at 08:14
  • It's at the top of your question. - [This is it](https://stackoverflow.com/questions/25198442/how-to-calculate-mean-median-per-group-in-a-dataframe-in-r) – Sotos Feb 09 '18 at 08:15

0 Answers0