[R] index vector
Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe - Share your photos with the people who matter at Yahoo! Canada Photos [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] index vector
Is this what you want? x - matrix(1:25,5) x[c(2,5),5] - - x [,1] [,2] [,3] [,4] [,5] [1,]16 11 1621 [2,]27 12 17 - [3,]38 13 1823 [4,]49 14 1924 [5,]5 10 15 20 - x[x[,5] == -, 5] - NA x [,1] [,2] [,3] [,4] [,5] [1,]16 11 16 21 [2,]27 12 17 NA [3,]38 13 18 23 [4,]49 14 19 24 [5,]5 10 15 20 NA On 9/29/06, bertrand toupin [EMAIL PROTECTED] wrote: Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe - Share your photos with the people who matter at Yahoo! Canada Photos [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@stat.math.ethz.ch 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.
Re: [R] index vector
On Fri, 2006-09-29 at 16:13 -0400, bertrand toupin wrote: Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe See ?is.na and note the use of: is.na(x) - value Example: mat - matrix(sample(50), 10, 5) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 19 [6,]7 36 31 49 37 [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 10 # Set some values in column 5 to -9 mat[sample(10, 3), 5] - -9 mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 30 5 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 11 6 [5,] 13 15 42 33 -9 [6,]7 36 31 49 -9 [7,] 29 419 27 4 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 -9 # Use which to get the indices within column 5 # of those values which are -9 # See ?which which(mat[, 5] == -9) [1] 5 6 10 # Now extend that and set those to NA is.na(mat[, 5]) - which(mat[, 5] == -9) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 NA [6,]7 36 31 49 NA [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 NA Note one other possibility, which is that if you used one of the read.table() family functions to read in a delimited ASCII file containing the data set, you can set the 'na.strings' argument to -9 and have it set these to NA upon importing. See ?read.table for more information. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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.
Re: [R] index vector
yes, exactly! Thanks a lot for your quick answer :) jim holtman [EMAIL PROTECTED] wrote: Is this what you want? x - matrix(1:25,5) x[c(2,5),5] - - x [,1] [,2] [,3] [,4] [,5] [1,]16 11 1621 [2,]27 12 17 - [3,]38 13 1823 [4,]49 14 1924 [5,]5 10 15 20 - x[x[,5] == -, 5] - NA x [,1] [,2] [,3] [,4] [,5] [1,]16 11 16 21 [2,]27 12 17 NA [3,]38 13 18 23 [4,]49 14 19 24 [5,]5 10 15 20 NA On 9/29/06, bertrand toupin wrote: Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe - Share your photos with the people who matter at Yahoo! Canada Photos [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? - Make free worldwide PC-to-PC calls. Try the new Yahoo! Canada Messenger with Voice [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] index vector
Thanks to you also. This is very informative and it helps me with some basics R function :) Marc Schwartz (via MN) [EMAIL PROTECTED] wrote: On Fri, 2006-09-29 at 16:13 -0400, bertrand toupin wrote: Hi! 1st time I'm posting here. I'm beginning to learn R and I've encountered a problem that I'm unable to solve so far. I have a 20 000 x 5 matrix. In the 5th column, I have elevation. Missing value are actually put to -9. I want to track down the index of those values and replace them with NA. I've read that to replace, the command replace is enough. I just don't know how to construct the index vector that contains the index of -9 values. Hope this makes sense, Thanks! Philippe See ?is.na and note the use of: is.na(x) - value Example: mat - matrix(sample(50), 10, 5) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 19 [6,]7 36 31 49 37 [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 10 # Set some values in column 5 to -9 mat[sample(10, 3), 5] - -9 mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 30 5 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 11 6 [5,] 13 15 42 33 -9 [6,]7 36 31 49 -9 [7,] 29 419 27 4 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 -9 # Use which to get the indices within column 5 # of those values which are -9 # See ?which which(mat[, 5] == -9) [1] 5 6 10 # Now extend that and set those to NA is.na(mat[, 5]) - which(mat[, 5] == -9) mat [,1] [,2] [,3] [,4] [,5] [1,] 24 39 40 305 [2,]8 443 34 47 [3,] 23 12 16 14 45 [4,] 35 262 116 [5,] 13 15 42 33 NA [6,]7 36 31 49 NA [7,] 29 419 274 [8,] 481 22 25 17 [9,] 43 32 28 38 20 [10,] 18 50 46 21 NA Note one other possibility, which is that if you used one of the read.table() family functions to read in a delimited ASCII file containing the data set, you can set the 'na.strings' argument to -9 and have it set these to NA upon importing. See ?read.table for more information. HTH, Marc Schwartz - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.