[R] removing missing values from a matrix
jim holtman
jholtman at gmail.com
Fri Oct 2 11:19:15 CEST 2009
Here is one way to do it. BTW 'a'-'z' won't work because there are 41
splits. Use a list instead:
> red <- matrix(seq(23688 * 164), nrow=23688, ncol=164)
> # create indices
> indx <- split(1:164, cut(1:164, 41))
> # now split matrix
> newMatrix <- lapply(indx, function(x) red[, x])
>
> str(newMatrix)
List of 41
$ (0.837,4.82]: int [1:23688, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
$ (4.82,8.8] : int [1:23688, 1:4] 94753 94754 94755 94756 94757
94758 94759 94760 94761 94762 ...
$ (8.8,12.8] : int [1:23688, 1:4] 189505 189506 189507 189508 189509
189510 189511 189512 189513 189514 ...
$ (12.8,16.8] : int [1:23688, 1:4] 284257 284258 284259 284260 284261
284262 284263 284264 284265 284266 ...
$ (16.8,20.8] : int [1:23688, 1:4] 379009 379010 379011 379012 379013
379014 379015 379016 379017 379018 ...
$ (20.8,24.7] : int [1:23688, 1:4] 473761 473762 473763 473764 473765
473766 473767 473768 473769 473770 ...
$ (24.7,28.7] : int [1:23688, 1:4] 568513 568514 568515 568516 568517
568518 568519 568520 568521 568522 ...
$ (28.7,32.7] : int [1:23688, 1:4] 663265 663266 663267 663268 663269
663270 663271 663272 663273 663274 ...
$ (32.7,36.7] : int [1:23688, 1:4] 758017 758018 758019 758020 758021
758022 758023 758024 758025 758026 ...
$ (36.7,40.7] : int [1:23688, 1:4] 852769 852770 852771 852772 852773
852774 852775 852776 852777 852778 ...
$ (40.7,44.7] : int [1:23688, 1:4] 947521 947522 947523 947524 947525
947526 947527 947528 947529 947530 ...
On Thu, Oct 1, 2009 at 6:57 PM, Amit Kumar <amitkumartiwary at gmail.com> wrote:
> Thanks!
> It worked! There is another problem I want to subset the matrix 'red'
> in following manner:
>>dim(red)
> 23688 164
>>a=red[1:23688,1:4]
>>b=red[1:23688,5:8]
>>c=red[1:23688,9:12]
> ..............................
> ..............................
>>z=red[1:23688,161:164]
> If there any efficient way to do it?
> cheers!
> Amit
>
>>
>> On Thu, 1 Oct 2009, Amit Kumar wrote:
>>
>>> Hi! All,
>>> I am working with a large matrix of dimension 23689 x 162. Some of the
>>> values of this matrix is missing (NA). And it looks something like
>>> that:
>>>
>>>> dim(red)
>>>
>>> 23689 162
>>>
>>>> red
>>>
>>> [,1] [,2] [,3] [,4] [,5]
>>> [1,] 2 NA 4 9 6
>>> [2,] 5 NA 6 NA 1
>>> [3,] NA 2 11 23 20
>>> [4,] 2 1 21 NA 3
>>> [5,] NA 7 NA 52 NA
>>>
>>> Here I want to convert NA to zero everywhere in the matrix. I do no
>>> want to omit NA using na.omit(red). I want output something like that:
>>>>
>>>> red
>>>
>>> [,1] [,2] [,3] [,4] [,5]
>>> [1,] 2 0 4 9 6
>>> [2,] 5 0 6 0 1
>>> [3,] 0 2 11 23 20
>>> [4,] 2 1 21 0 3
>>> [5,] 0 7 0 52 0
>>>
>>> Please, help thanks.
>>> Amit
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list