I found the text of certain cell entries in my dataframe to be garbled and would like to replace them with string, but R returns the following message
#load data from dropbox
library(foreign)
data <- read.csv("https://www.dropbox.com/s/anm8xrovxc5xtr5/comtrade2009.csv?dl=1")
unique(data$ptTitle)[75]
[1] <NA>
#this is not an NA because the text on the CSV file appears to be some garbled string due to encoding,
#it shows "C<U+00F4>te d'Ivoire"
data$ptTitle[data$ptTitle == <NA>] <- "Cote d'Ivoire"
Warning message:
In `[<-.factor`(`*tmp*`, ct2009$ptTitle == "<NA>", value = c(238L, :
invalid factor level, NA generated
it does not allow me to replace those garbled character values with character string, does anyone know how to overwrite those garbled characters with my preferred character string?
Update
So I guess a better way to work around this is to add stringsAsFactors=F
when loading csv file using read.csv
, so it's much easier to replace cell values with NA
(instead of <NA>
).
Sorry for all the hassles this thread might have caused.