Excerpts from Zahra via R-help's message of 2015-11-02 17:49:01 -0200:
> Hi there,
>
> I am looking for some help replacing missing values in R with the row mean.
> This is survey data and I am trying to impute values for missing variables in
> each set of questions separately using the mean of
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon
Sent: Monday, November 2, 2015 5:33 PM
To: Zahra
Cc: r-help mailing list
Subject
Hi again,
Small typo in line 5 - should be
replace_NAs<-function(x,group_lab=c("A","B","C")) {
for(lab in group_lab) {
indices<-grep(lab,names(x),fixed=TRUE)
na_indices<-is.na(x[indices])
if(any(na_indices))
x[indices][na_indices]<-rowMeans(x[indices],na.rm=TRUE)
}
return(x)
}
Jim
Hi Zahra,
I can't think of an "apply" function that will do this, but:
Zdf<-read.table(text="ID A1 A2 A3 B1 B2 B3 C1 C2 C3 C4
b 4 5 NA2 NA 4 5 13
NA
c 4 5 1 NA 34 5 13
2
4 matches
Mail list logo