So I have a data frame in R that contains integers, NA’s, and a random assortment of strings inside the cells. Only one data type per cell. What I’m wondering is how to change all of the cells that contain strings into NA. Any idea how I could do this?
Share
If your data frame (df) is really all integers except for NAs and garbage then then the following converts it.
You’ll have a warning about NAs introduced by coercion but that’s just all those non numeric character strings turning into NAs.
The following code also works and is more concise but runs slower.
If you just want to convert selected columns then you could use a slightly more complicated command. First you need to figure out which columns you want to convert. Perhaps you save them as a logical vector of the columns you wish to change.
This would knock things out of order but it would get you what you want easy enough.