I have a a variable in a dataframe whose observations are a mix of numeric and character values (due to faulty data entry). How can I subset in only the observations which are numeric? Suppose the values of filename$varname are (1, 2, 1, 5, 3, a, 3, d, 1), I would like subset out “a” and “d” and keep only the rest of the values which are numeric.
Share
Without a reproducible example it is hard to see what your data actually looks like. For instance, is the column of your data frame a factor or just strings? If it is just strings then Andrie’s answer works (just use
as.numeric()), and if the data is a factor you first need to convert that to strings withas.character(x):You will get some
NAs but that is absolutely fine as those values are indeed missing.EDIT: To clarify abit more. You have a data frame, so you don’t want to take values out of the data frame as then it wouldn’t be a dataframe anymore (equal rows). You want to correctly assign
NAfor missing values instead as most statistical functions in R can handle them.