[R] how to replace <NA> values

PIKAL Petr petr.pikal at precheza.cz
Mon Jan 20 15:51:37 CET 2014


Hi

I believe that it is not what is wanted (if the OP knows exactly what he whants, which I doubt).

your result is

> Elder1
   ID age
1 ID1   3
2 ID2   2
3 ID3   1
4 ID6   0
5 ID8   0
>

however original output shall be

Elder1$age[Elder1$age==""]<-NA
Elder1$age<-as.numeric(as.character(Elder1$age))
Elder1$age[is.na(Elder1$age)]<-0
Elder1
   ID age
1 ID1  38
2 ID2  35
3 ID3   0
4 ID6   0
5 ID8   0
>

It is up to the OP to understand what age = 0 means.

Regards
Petr



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of John Sorkin
> Sent: Monday, January 20, 2014 3:07 PM
> To: r-help at r-project.org; ecokingsly at yahoo.co.in
> Subject: Re: [R] how to replace <NA> values
>
> I hope the code below will show you how to co what you want to do.
> John
>
> Elder1 <- data.frame(
>   ID=c("ID1","ID2","ID3","ID6","ID8"),
>   age=c(38,35,"",NA,NA))
> Elder1
> # The age variable is a factor, we want it to be numeric
> class(Elder1[,'age'])
> # Convert factor to a numeric value.
> Elder1$age<-as.numeric(Elder1[,'age'])
> # Below you will see how you can test a value to see if it is NA.
> is.na(Elder1[,'age'])
> # Replace the NA values with a zero.
> Elder1[is.na(Elder1[,'age']),"age"]<-0
> # The final data frame with NAs replaced by zeros.
> Elder1
>
>
>
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and
> Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street
> GRECC (BT/18/GR) Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> >>> kingsly <ecokingsly at yahoo.co.in> 01/19/14 3:01 PM >>>
> Dear R community
>
> I have a large data set contain some empty cells. Because of that,  may
> be I am wrong, <NA> values are produced.
> Now I want replace both empty and <NA> values with zero.
>
> Elder1 <- data.frame(
>   ID=c("ID1","ID2","ID3","ID6","ID8"),
>   age=c(38,35,"",NA,NA))
> Output I am expecting
>
> ID   age
> ID1  38
> ID2  35
> ID3  0
> ID6  0
> ID8  0
>
> In advance I thank your help.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-
> replace-NA-values-tp4683831.html
> Sent from the R help mailing list archive at Nabble.com.
>     [[alternative HTML version deleted]]
>
>
>
> Confidentiality Statement:
> This email message, including any attachments, is for the sole use of
> the intended recipient(s) and may contain confidential and privileged
> information.  Any unauthorized use, disclosure or distribution is
> prohibited.  If you are not the intended recipient, please contact the
> sender by reply email and destroy all copies of the original message.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list