I have a dataframe in R:
df <- data.frame(CRP = c(“10”, “2”, “3”, NA, “<4”, “>5”))
I want to create a numeric column that replaces the strings into numeric values. Additionally, whenever it find a string starting with “<“ or “>”, it should look in the next letter, i.e. number, and impute the string with the median of the observations above or below that number. For example, “<5” should be replaced with median(c(2,3)). The remaining NAs should remain as it is currently.
Thanks in advance
I have not managed to find a solution that avoids pre specifying functions a priori. I also want to avoid for loops. Ideally I would like to apply this into a data frame of millions of rows where multiple columns have that issue.