r - How to change characters into NA? -


i have census dataset missing variables indicated ?, when checking incomplete cases in r says there none because r takes ? valid character. there way change ? nas? run multiple imputation using mice package fill in missing data after.

creating data frame df

df <- data.frame(a=c("?",1,2),b=c(2,3,"?")) df #   b # 1 ? 2 # 2 1 3 # 3 2 ? 

i. using replace() function

replace(df,df == "?",na) #         b # 1 <na>    2 # 2    1    3 # 3    2 <na> 

ii. while importing file ?

 data <- read.table("xyz.csv",sep=",",header=t,na.strings=c("?",na))  data  #  b  # 1  1 na  # 2  2  3  # 3  3  4  # 4 na na  # 5 na na  # 6  4  5 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -