replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

lida9011
Hello specialist,

I have a matrix in which there are NA,0,1 and 2 in each columns.
I wanna replace NAs with special proportion of 0,1 or 2 !
for example in df<- matric(df, nrow=50, ncol=100)

If in one column the number of NAs = 10 , # of 0=50 , #of 1=25  and # of
2=15
I want to replace  5 of 10 NAs with 0 , 3 NAs with 1 and 2 NAs with 2!


I've already calculated the proportion of 0, 1 and 2 for each column, just
I don't know how to replace the number of NAs with these number (0,1,2) and
specific proportion?

Thank you very much in advance

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

Henrique Dallazuanna
Maybe you can use something like this

In this way, almost your proportion of 0, 1 and 2 will be maintained

m <- matrix(sample(c(NA, 0:2), size = 50*100, replace = TRUE), nrow = 50,
ncol = 100)
trunc(prop.table(apply(m, 2, table), 2) * colSums(is.na(m)), 0)
m[is.na(m)] <- unlist(apply(trunc(prop.table(apply(m, 2, table), 2) *
colSums(is.na(m)), 0), 2, rep, x = 0:2))

On Mon, Jun 6, 2016 at 5:38 PM, Lida Zeighami <[hidden email]> wrote:

> Hello specialist,
>
> I have a matrix in which there are NA,0,1 and 2 in each columns.
> I wanna replace NAs with special proportion of 0,1 or 2 !
> for example in df<- matric(df, nrow=50, ncol=100)
>
> If in one column the number of NAs = 10 , # of 0=50 , #of 1=25  and # of
> 2=15
> I want to replace  5 of 10 NAs with 0 , 3 NAs with 1 and 2 NAs with 2!
>
>
> I've already calculated the proportion of 0, 1 and 2 for each column, just
> I don't know how to replace the number of NAs with these number (0,1,2) and
> specific proportion?
>
> Thank you very much in advance
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

Bert Gunter-2
In reply to this post by lida9011
Sidenote:

When you do imputation in this way all your inference (error
estimates, goodness of fit, confidence intervals, etc.) will be wrong
and misleading, as you are creating "information" from nothing.

If this comment is irrelevant, please ignore.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Jun 6, 2016 at 1:38 PM, Lida Zeighami <[hidden email]> wrote:

> Hello specialist,
>
> I have a matrix in which there are NA,0,1 and 2 in each columns.
> I wanna replace NAs with special proportion of 0,1 or 2 !
> for example in df<- matric(df, nrow=50, ncol=100)
>
> If in one column the number of NAs = 10 , # of 0=50 , #of 1=25  and # of
> 2=15
> I want to replace  5 of 10 NAs with 0 , 3 NAs with 1 and 2 NAs with 2!
>
>
> I've already calculated the proportion of 0, 1 and 2 for each column, just
> I don't know how to replace the number of NAs with these number (0,1,2) and
> specific proportion?
>
> Thank you very much in advance
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.