[R] how to replace <NA> values

John Sorkin JSorkin at grecc.umaryland.edu
Mon Jan 20 15:06:49 CET 2014


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. 


More information about the R-help mailing list