忽略样本函数中的值或NA

忽略样本函数中的值或NA

本文介绍了忽略样本函数中的值或NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个矩阵,我想从每一行中提取一个随机样本.我的一些数据在NA中,但是在抽取随机样本时,我不希望将NA作为抽样的选项.我该怎么办?

I have a matrix in R that I would like to take a single random sample from each row. Some of my data is in NA, but when taking the random sample I do not want the NA to be an option for the sampling. How would I accomplish this?

例如,

a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5)
a
     [,1] [,2] [,3] [,4] [,5]
[1,]    5    5   10   10   NA
[2,]    5    5   10   10   NA
[3,]    5    5   10   10   NA
[4,]    5    5   10   10   NA
[5,]    5    5   10   10   NA

当我将样本函数应用于此矩阵以输出另一个矩阵时,我得到

When I apply the sample function to this matrix to output another matrix I get

b <- matrix(apply(a, 1, sample, size=1), ncol=1)
b

     [,1]
[1,]   NA
[2,]   NA
[3,]   10
[4,]   10
[5,]    5

相反,我不希望NA能够作为输出,并且希望输出类似于:

Instead I do not want the NA to be capable of being the output and want the output to be something like:

b
     [,1]
[1,]   10
[2,]   10
[3,]   10
[4,]    5
[5,]   10

推荐答案

也许有更好的方法,但是sample似乎没有与NA相关的任何参数,因此我只写了一个匿名函数来处理NA.

There might be a better way but sample doesn't appear to have any parameters related to NAs so instead I just wrote an anonymous function to deal with the NAs.

apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})

本质上是您想要的.如果您真的想要矩阵输出,可以这样做

essentially does what you want. If you really want the matrix output you could do

b <- matrix(apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)}), ncol = 1)


您没有要求这样做,但是我提出的解决方案在某些情况下确实失败了(主要是如果一行仅包含NA.


You didn't ask for this but my proposed solution does fail in certain cases (mainly if a row contains ONLY NAs.

a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5)
# My solution works fine with your example data
apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})

# What happens if a row contains only NAs
a[1,] <- NA

# Now it doesn't work
apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})

# We can rewrite the function to deal with that case
mysample <- function(x, ...){
    if(all(is.na(x))){
        return(NA)
    }
    return(sample(x[!is.na(x)], ...))
}

# Using the new function things work.
apply(a, 1, mysample, size = 1)

这篇关于忽略样本函数中的值或NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:24