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! John [[alternative HTML version deleted]]
David Winsemius
2010-May-25 13:01 UTC
[R] Assigning NA to a rows of a dataframe/datamatrix
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