dataset - Aggregate function only occasionally working in R -


i doing analysis on 2 datasets split different countries (same both datasets different numbers) 3 of countries there missing data. using aggregate() function fill in dummy values can analysis without nas popping up. reason function won't work when merging new values original data.

but if clear workspace , run again might work 1 or 2 of countries, or 1 of 2 datasets. can't understand why may work 1 time not another, when i'm not changing code time. appreciated.

mil<-read.csv("c:/data_millions.csv",header=true) per<-read.csv("c:/data_percent.csv",header=true)  ##fill in blanks za #create dummy numbers each category of age/age-gender aggregate(data=mil,za~typeofperson,mean,na.rm=true) #merge output original data ave_za<-ave(mil$za,mil$typeofperson,fun=function(x)mean(x,na.rm=true)) mil$za<-ifelse(is.na(mil$za),ave_za,mil$za)  aggregate(data=per,za~typeofperson,mean,na.rm=true) ave_za_per<-ave(per$za,per$typeofperson,fun=function(x)mean(x,na.rm=true)) per$za<-ifelse(is.na(per$za),ave_za_per,per$za)  ##fill in blanks bewa aggregate(data=mil,bewa~typeofperson,mean,na.rm=true) ave_bewa<-ave(mil$bewa,mil$typeofperson,fun=function(x)mean(x,na.rm=true)) mil$bewa<-ifelse(is.na(mil$bewa),ave_bewa,mil$bewa)  aggregate(data=per,bewa~typeofperson,mean,na.rm=true) ave_bewa_per<-ave(per$bewa,per$typeofperson,fun=function(x)mean(x,na.rm=true)) per$bewa<-ifelse(is.na(per$bewa),ave_za_per,per$bewa)  ##fill in blanks gr aggregate(data=mil,gr~typeofperson,mean,na.rm=true) ave_gr<-ave(mil$gr,mil$typeofperson,fun=function(x)mean(x,na.rm=true)) mil$gr<-ifelse(is.na(mil$gr),ave_gr,mil$gr)  aggregate(data=per,gr~typeofperson,mean,na.rm=true) ave_gr_per<-ave(per$gr,per$typeofperson,fun=function(x)mean(x,na.rm=true)) per$gr<-ifelse(is.na(per$gr),ave_gr_per,per$gr) 

update: example data , has not worked

here there still nas: https://www.dropbox.com/s/bd9c9mjttdehbrt/missing.jpg?dl=0

here link data: https://www.dropbox.com/s/vsiq9nr6ic3odmv/data_millions.csv?dl=0


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -