[R] Assigning NA to a rows of a dataframe/datamatrix
David Winsemius
dwinsemius at comcast.net
Tue May 25 15:01:56 CEST 2010
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
More information about the R-help
mailing list