s-news
[Top] [All Lists]

Re: Simple NA problem

To: Jan Ivanouw <ivanouw@post8.tele.dk>
Subject: Re: Simple NA problem
From: Chuck Cleland <ccleland@optonline.net>
Date: Mon, 31 Dec 2007 06:30:46 -0500
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <000c01c84b9e$b0bde4d0$6401a8c0@JansAthlon64>
References: <20071230052101.JLNK22041.fep22.mail.dk@smtp.biostat.wustl.edu> <000c01c84b9e$b0bde4d0$6401a8c0@JansAthlon64>
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)
Jan Ivanouw wrote:
> I am sorry I can't find the solution to this simple problem. Can anyone
> help?
>  
> I have a (large) data frame with missing values, like this:
>  
> a       b     c
> 1       3     5
> NA    9     2
> 3       5     2
> NA    8     6
> 5       3     7
>  
> Based on non-missing values of variable a, I want it changed to
>  
> a     b     c
> 1     3     5
> 3     5     2
> 5     3     7
>  
>  
> Thank you
>  
> Jan Ivanouw PhD
> University of Copenhagen
> Denmark

  Does this help?

df <- data.frame(a = c(1, NA, 3, NA, 5),
                 b = c(3,  9, 5,  8, 3),
                 c = c(5,  2, 2,  6, 7))

df
   a b c
1  1 3 5
2 NA 9 2
3  3 5 2
4 NA 8 6
5  5 3 7

newdf <- df[!is.na(df$a),]

newdf
  a b c
1 1 3 5
3 3 5 2
5 5 3 7

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

<Prev in Thread] Current Thread [Next in Thread>