[R] Changing NA to 0 in selected columns of a dataframe

arun smartpink111 at yahoo.com
Fri Oct 12 01:17:30 CEST 2012



Hi Michael,

Sorry!
 You are right. That was OP's code, which I cut and paste without noticing it.  

To Scoyoc:
You can also try this:
dat1 <-data.frame(A, B, C, D, E ) 
dat1new<-dat1[,2:3]
 dat1new[is.na(dat1new)]<-0
 dat1[,2:3]<-dat1new
 dat1
#  A B  C  D  E
#1 1 6  0 16 21
#2 2 7  0 17 NA
#3 3 0 13 18 NA
#4 4 0 14 19 NA
#5 5 0 15 20 25

A.K.


----- Original Message -----
From: R. Michael Weylandt <michael.weylandt at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: scoyoc <scoyoc at gmail.com>; R help <r-help at r-project.org>
Sent: Thursday, October 11, 2012 7:04 PM
Subject: Re: [R] Changing NA to 0 in selected columns of a dataframe

On Thu, Oct 11, 2012 at 11:58 PM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
> Try this:
>  dat1 = as.data.frame ( cbind ( A, B, C, D, E ) )

No. Do not try this. It is a Very Bad Thing to use

as.data.frame(cbind(...))

instead of

data.frame(...)

for reasons I've mentioned before on this list. In short, cbind()
forces all its arguments to a single mode, thereby missing the entire
point of a data frame.

Cheers,
Michael





More information about the R-help mailing list