Re: [R] replace NA's with row means for specific columns

2015-11-12 Thread Marco
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

Re: [R] replace NA's with row means for specific columns

2015-11-03 Thread David L Carlson
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

Re: [R] replace NA's with row means for specific columns

2015-11-02 Thread Jim Lemon
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

Re: [R] replace NA's with row means for specific columns

2015-11-02 Thread Jim Lemon
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

[R] replace NA's with row means for specific columns

2015-11-02 Thread Zahra via R-help
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 the scores for the other questions within that set. I have a dataset that look