Hi, I had a matrix with NULL values, which I wanted to replace with NA. Is there an efficient way to do this? Small sample input matrix: A B C D E 1 5222.18 6355.10 4392.68 2607.41 4524.09 2 NULL 257.33 NULL 161.51 119.44 3 NULL 274.80 305.28 443.27 NULL 4 1759.76 1556.45 1224.06 1558.73 1837.09 Tim ____________________________________________________________________________________ Be a better friend, newshound, and [[alternative HTML version deleted]]
Tim Smith wrote:> Hi, > > I had a matrix with NULL values, which I wanted to replace with NA. Is there an efficient way to do this? > > Small sample input matrix: > A B C D E > 1 5222.18 6355.10 4392.68 2607.41 4524.09 > 2 NULL 257.33 NULL 161.51 119.44 > 3 NULL 274.80 305.28 443.27 NULL > 4 1759.76 1556.45 1224.06 1558.73 1837.09How is it possible to construct such a matrix? Uwe Ligges> > Tim > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > [[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.
Tim Smith wrote:> Hi, > > I had a matrix with NULL values, which I wanted to replace with NA. Is there an efficient way to do this? > > Small sample input matrix: > A B C D E > 1 5222.18 6355.10 4392.68 2607.41 4524.09 > 2 NULL 257.33 NULL 161.51 119.44 > 3 NULL 274.80 305.28 443.27 NULL > 4 1759.76 1556.45 1224.06 1558.73 1837.09 > > > TimIf you are reading this in from a text file, you can use: mat <- as.matrix(read.table(YourFileName, header = TRUE, na.strings = "NULL")) See the 'na.strings' argument in ?read.table Note that by default, this will be read in as a data frame, not a matrix, so I added the extra coercion. The difference is quite important. The above should yield: > mat A B C D E 1 5222.18 6355.10 4392.68 2607.41 4524.09 2 NA 257.33 NA 161.51 119.44 3 NA 274.80 305.28 443.27 NA 4 1759.76 1556.45 1224.06 1558.73 1837.09 > str(mat) num [1:4, 1:5] 5222 NA NA 1760 6355 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:4] "1" "2" "3" "4" ..$ : chr [1:5] "A" "B" "C" "D" ... HTH, Marc Schwartz
if your matrix is stored in a text file then xx <- read.table("x", na.strings="NULL") where "x" is the name of the text file. On Thu, 17 Apr 2008, Tim Smith wrote:> Hi, > > I had a matrix with NULL values, which I wanted to replace with NA. Is there an efficient way to do this? > > Small sample input matrix: > A B C D E > 1 5222.18 6355.10 4392.68 2607.41 4524.09 > 2 NULL 257.33 NULL 161.51 119.44 > 3 NULL 274.80 305.28 443.27 NULL > 4 1759.76 1556.45 1224.06 1558.73 1837.09 > > > Tim > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > [[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. >
On Thu, 17 Apr 2008, Uwe Ligges wrote:> Tim Smith wrote: >> Hi, >> >> I had a matrix with NULL values, which I wanted to replace with NA. Is there an efficient way to do this? >> >> Small sample input matrix: >> A B C D E >> 1 5222.18 6355.10 4392.68 2607.41 4524.09 >> 2 NULL 257.33 NULL 161.51 119.44 >> 3 NULL 274.80 305.28 443.27 NULL >> 4 1759.76 1556.45 1224.06 1558.73 1837.09 > > > How is it possible to construct such a matrix?> A <- matrix(list(5222.18, NULL, NULL, 1759.76), 4, 1) > A[,1] [1,] 5222.18 [2,] NULL [3,] NULL [4,] 1759.76 A[sapply(A, is.null)] <- NA A [,1] [1,] 5222.18 [2,] NA [3,] NA [4,] 1759.76 -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595