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 ?
na
s? 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
Post a Comment