In my original data a csv file I have missing values. If I use read.table the missing values are replaced by NAs. Is it possible to get object where missing values aren't replaced with NAs? Is it possible to replace NAs with empty space? [[alternative HTML version deleted]]
On Wed, Feb 17, 2016 at 12:04 PM, Jan Kacaba <jan.kacaba at gmail.com> wrote:> In my original data a csv file I have missing values. If I use read.table > the missing values are replaced by NAs. >That is the normal way of dealing with missing values, in R. Is it possible to get object where missing values aren't replaced with NAs?> Is it possible to replace NAs with empty space? >It is possible to replace the NAs with empty space, but nobody would recommend that because your variable will be coerced to a character mode. If the other values are numbers, you won't be able to compute any numerical measures. Try to accept that NA is there for a reason, in R. I hope this helps, Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
Beside of Adrian's answer, R can simply manage NAs but you need to deal with "empty space" on your own. See ?is.na ?na.omit Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Adrian > Du?a > Sent: Wednesday, February 17, 2016 2:01 PM > To: Jan Kacaba > Cc: r-help at r-project.org > Subject: Re: [R] missing values in csv file > > On Wed, Feb 17, 2016 at 12:04 PM, Jan Kacaba <jan.kacaba at gmail.com> > wrote: > > > In my original data a csv file I have missing values. If I use > > read.table the missing values are replaced by NAs. > > > > That is the normal way of dealing with missing values, in R. > > > Is it possible to get object where missing values aren't replaced with > NAs? > > Is it possible to replace NAs with empty space? > > > > It is possible to replace the NAs with empty space, but nobody would > recommend that because your variable will be coerced to a character > mode. > If the other values are numbers, you won't be able to compute any > numerical measures. > Try to accept that NA is there for a reason, in R. > > I hope this helps, > Adrian > > -- > Adrian Dusa > University of Bucharest > Romanian Social Data Archive > Soseaua Panduri nr.90 > 050663 Bucharest sector 5 > Romania > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ 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.
Assuming it is a character variable test <- c("a", NA, "b") > test [1] "a" NA "b" > test[is.na(test)] <- " " > test [1] "a" " " "b" but if it is numeric this as, as others have said, almost certainly not what you really wanted to do On 17/02/2016 10:04, Jan Kacaba wrote:> In my original data a csv file I have missing values. If I use read.table > the missing values are replaced by NAs. > > Is it possible to get object where missing values aren't replaced with NAs? > Is it possible to replace NAs with empty space? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
> Is it possible to get object where missing values aren't replaced with NAs?Not with read.table, if they are really missing. But you can replace them later - see below - and if they are marked you can change what read.table considers to be 'missing'.> Is it possible to replace NAs with empty space?NA _is_ an 'empty space', in the sense of a correctly recorded missing value. If you mean some other kind of empty space - perhaps the string "" - that's not missing, just empty (!). But with a vector containing NA's, like ( x <- c(NA, NA, letters[1:5]) ) you can do things like x[is.na(x)] <- "Empty Space" x or x[is.na(x)] <- "" S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
You can add the argument na.print=" " to print() to make the missing values print as " " instead of as NA. Some, but not all print methods support this. E.g., > print(c(1,2,NA,4,5,NA), na.print="-") [1] 1 2 - 4 5 - > print(matrix(c(1,2,NA,4,5,NA),2), na.print="-") [,1] [,2] [,3] [1,] 1 - 5 [2,] 2 4 - > # print.data.frame use na.print for factor/character data only > print(data.frame(X=c(1,NA,3), Y=LETTERS[c(NA,2,3)]), na.print="-")> print(data.frame(X=c(1,NA,3), Y=LETTERS[c(NA,2,3)], Z=c(NA,TRUE,FALSE)),na.print="-") X Y Z 1 1 - NA 2 NA B TRUE 3 3 C FALSE In write.table (& write.csv) the argument na=" " does the trick. > write.csv(data.frame(X=c(1,NA,3), Y=LETTERS[c(NA,2,3)], Z=c(NA,TRUE,FALSE)), na="-") "","X","Y","Z" "1",1,-,- "2",-,"B",TRUE "3",3,"C",FALSE Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Feb 17, 2016 at 2:04 AM, Jan Kacaba <jan.kacaba at gmail.com> wrote:> In my original data a csv file I have missing values. If I use read.table > the missing values are replaced by NAs. > > Is it possible to get object where missing values aren't replaced with NAs? > Is it possible to replace NAs with empty space? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]