I have a dataframe with values for each age between 0 and 110 for each year between 1867 and now. As I don't know how to describe better my data I joined 2 printscreen of it.
For each year, I need to take the age corresponding to the median of the column "lx". lx is a column representing the number of survivors in a given population for each year starting from 100000. So for each year I need to take the age for which we had 50.000 survivors.
I have been trying this code but it doesn't work, it gives me only one value :
I do not think you want to use the median() function if what you want is the age which has an lx value of 50000. Of course, it is unlikely that any age will have a value of exactly 50000. In the code below, I select for the year that has the lx value that is closest to 50000. Other possible choices would be the last age that has lx > 50000 or the first age that has lx < 50000.