On May 25, 2010, at 5:23 AM, john james wrote:

Dear R-users,  I have a problem, I have the following dataframe:

d<-data.frame(
 'y1'=c(1,2,1,2,1,NA,NA),
'y2'=c(1,2,1,1,1,2,1),
'y3'=c(1,NA,1,NA,NA,2,1),
'y4'=c(NA,2,NA,1,1,2,NA),
'a'=c(1,1,1,1,1,1,2)
)

where the last variable counts the number of missing values in a row. Now, i want to set rows where a>1 to NA and arrive at something like the following;

dnew<-data.frame(
 'y1'=c(1,2,1,2,1,NA,NA),
'y2'=c(1,2,1,1,1,2,NA),
'y3'=c(1,NA,1,NA,NA,2,NA),
'y4'=c(NA,2,NA,1,1,2,NA),
'a'=c(1,1,1,1,1,1,4)
)

Please, how do I go about this. Many thanks!

is.na(d[rowSums(is.na(d))>1, 1:4]) <- TRUE

If you want the column "a" to be updated you would need to recalculate the numbers of NA's

d[ , "a"] <- rowSums(is.na(d))

--
David

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to