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]]
Dear Kinsly, try http://bit.ly/1mhXOfH Although values are usually missing for some good reason. Changing them to 0 might be very dangerous, R is going to think that ID3, 6 and 8 are all newborn babies! best daniel ________________________________________ Felad?: r-help-bounces at r-project.org [r-help-bounces at r-project.org] ; meghatalmazó: kingsly [ecokingsly at yahoo.co.in] K?ldve: 2014. janu?r 19. 20:39 To: r-help at r-project.org T?rgy: [R] how to replace <NA> values 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]]
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.
Can someone provide suggestions about how to best set up an R server? I would like to be able to run R on my IPad. It sounds like the only way to do this is to have the IPad access an R server. The server will be at my home, connected to the internet via my cable company (comcast). I don't yet know if the server will be a linux box or a windows box. I would appreciate advice about setting up both kinds of servers. Thank you, John 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) 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.
On Sun, 19 Jan 2014 11:39:43 -0800 (PST) kingsly <ecokingsly at yahoo.co.in> wrote:> 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. >The age variable is being read in as a factor because of the "". If you were to replace it with NA, the type becomes numerical: Before replacement: str(Elder1) 'data.frame': 5 obs. of 2 variables: $ ID : Factor w/ 5 levels "ID1","ID2","ID3",..: 1 2 3 4 5 $ age: Factor w/ 3 levels "","35","38": 3 2 1 NA NA Notice that the "" is treated as a factor level. After: str(Elder1) 'data.frame': 5 obs. of 2 variables: $ ID : Factor w/ 5 levels "ID1","ID2","ID3",..: 1 2 3 4 5 $ age: num 38 35 NA NA NA SO, the question, is what do you want to do with that column? An "NA" value tells you honestly that the information is missing. Replacing it with a zero can be misleading and can bias some basic parameter estimates. After you know how you want to treat the data in that field, you may have a better idea of how to handle the missing data. JWD
I'm pretty sure that what you have is not what you think you have. Do a str(Elder1) on the data set and you will see that age is a factor not a numerical variable. Bascally you have a mangled data set from the look of it. Why would you want to change NA's to 0's? John Kane Kingston ON Canada> -----Original Message----- > From: ecokingsly at yahoo.co.in > Sent: Sun, 19 Jan 2014 11:39:43 -0800 (PST) > To: r-help at r-project.org > Subject: [R] how to replace <NA> values > > Dear R community > B > I have a large data set contain some empty cells. BecauseB of that,B may > be I am wrong, <NA> values are produced. > Now I want replace both empty and <NA> values with zero. > B > Elder1 <- data.frame( > B ID=c("ID1","ID2","ID3","ID6","ID8"), > B age=c(38,35,"",NA,NA)) > Output I am expecting > B > IDB B age > ID1B 38 > ID2B 35 > ID3B 0 > ID6B 0 > ID8B 0 > B > 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]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.____________________________________________________________ Send your photos by email in seconds... TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if3 Works in all emails, instant messengers, blogs, forums and social networks.
Maybe Matching Threads
- Matrix problem to extract animal associations
- Wishlist: merge and subset to keep attributes (PR#8658)
- Recoding variables based on reference values in data frame
- colnames for data.frame could be greatly improved
- How to check if a value of a variable is in a list