karena
2010-Jan-12 17:46 UTC
[R] how to handle missing values "." when importing data in R
hi, I have a question about importing data in R. I want to import a file which has missing value in it, and the missing values are denoted as ".", I want to first read in the file, and then change the "." into the number zero "0". how can I do that? thank you, karena -- View this message in context: http://n4.nabble.com/how-to-handle-missing-values-when-importing-data-in-R-tp1012298p1012298.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2010-Jan-12 17:56 UTC
[R] how to handle missing values "." when importing data in R
?read.table na.strings='.' Then change all NAs to zero df$col[is.na(df$col)] <- 0 On Tue, Jan 12, 2010 at 12:46 PM, karena <dr.jzhou@gmail.com> wrote:> > hi, I have a question about importing data in R. > > I want to import a file which has missing value in it, and the missing > values are denoted as ".", I want to first read in the file, and then > change > the "." into the number zero "0". > > how can I do that? > > thank you, > > karena > -- > View this message in context: > http://n4.nabble.com/how-to-handle-missing-values-when-importing-data-in-R-tp1012298p1012298.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
(Ted Harding)
2010-Jan-12 18:42 UTC
[R] how to handle missing values "." when importing data in
On 12-Jan-10 17:46:47, karena wrote:> hi, I have a question about importing data in R. > > I want to import a file which has missing value in it, and the missing > values are denoted as ".", I want to first read in the file, and then > change the "." into the number zero "0". > > how can I do that? > > thank you, > > karenaIt may depend on what format the file is in, but if it is a tabular text file or a CSV file then you can use the "na.strings" parameter. Here is an example of a little CSV file with "." used for "missing": file temp.csv: -------------- A,B,C,D 1.1,1.2,1.3,1.4 2.1,2.2,.,2.4 3.1,.,3.3,3.4 4.1,.,.,4.4 D <- read.csv("temp.csv",na.strings=".") D # A B C D # 1 1.1 1.2 1.3 1.4 # 2 2.1 2.2 NA 2.4 # 3 3.1 NA 3.3 3.4 # 4 4.1 NA NA 4.4 So the "." have gone in as NA (the right thing to do in the first instance with missing data). Now you can replace these by zeros: D[is.na(D)] <- 0 D # 1 1.1 1.2 1.3 1.4 # 2 2.1 2.2 0.0 2.4 # 3 3.1 0.0 3.3 3.4 # 4 4.1 0.0 0.0 4.4 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 12-Jan-10 Time: 18:42:40 ------------------------------ XFMail ------------------------------