按行顺序识别NA [英] Identify NA's in sequence row-wise

查看:52
本文介绍了按行顺序识别NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据条件按行顺序填充NA值.请参见下面的示例.

I want to fill NA values in a sequence, which is row-wise, based on a condition. Please see example below.

ID | Observation 1 | Observation 2 | Observation 3 | Observation 4 | Observation 5
 A         NA              0               1             NA             NA

条件是:

  • 序列中!NA值之前的所有NA值均应保留为NA;
  • 但是序列中!NA值之后的所有NA都应加标签(删除")

在上面的示例中,观察值1中的NA值应保持为NA.但是,观察值4和5中的NA值应更改为删除".

In the example above, NA value in Observation 1 should remain NA. However, the NA values in Observations 4 and 5 should be changed to "Remove".

推荐答案

您可以定义函数:

replace.na <- function(r,val) {
  i <- is.na(r)
  j <- which(i)
  k <- which(!i)
  r[j[j > k[length(k)]]] <- val
  r
}

然后,假设您具有这样的data.frame:

Then, assuming that you have a data.frame like so:

r <- data.frame(ID=c('A','B'),obs1=c(NA,1),obs2=c(0,NA),obs3=c(1,2),obs4=c(NA,3),obs5=c(NA,NA))
##  ID obs1 obs2 obs3 obs4 obs5
##1  A   NA    0    1   NA   NA
##2  B    1   NA    2    3   NA

我们可以对r所有数字列的行上的apply函数:

We can apply the function over the rows for all numeric columns of r:

r[,-1] <- t(apply(r[,-1],1,replace.na,999))    
##  ID obs1 obs2 obs3 obs4 obs5
##1  A   NA    0    1  999  999
##2  B    1   NA    2    3  999

这会将r[,-1]视为matrix,并且apply的输出将填充matrix,默认情况下,该列将由列填充.因此,在将列替换回r之前,我们必须转置结果matrix.

This treats r[,-1] as a matrix and the output of apply fills a matrix, which by default is filled by columns. Therefore, we have to transpose the resulting matrix before replacing the columns back into r.

另一种呼叫replace.na的方法是:

r[,-1] <- do.call(rbind,lapply(data.frame(t(r[,-1])),replace.na,999))

在这里,我们首先转置r的数字​​列并将其设为data.frame.这使r的每一行成为列列表中的一列,该列即为结果数据帧.然后在这些列上使用lapply来应用replace.narbind结果.

Here, we transpose the numeric columns of r first and make that a data.frame. This makes each row of r a column in the list of columns that is the resulting data frame. Then use lapply over these columns to apply replace.na and rbind the results.

如果要标记第一个非NA之后的所有NA,则函数replace.na应该为:

If you want to flag all NA's after the first non-NA, then the function replace.na should be:

replace.na <- function(r,val) {
  i <- is.na(r)
  j <- which(i)
  k <- which(!i)
  r[j[j > k[1]]] <- val
  r
}

将其应用于数据:

r[,-1] <- do.call(rbind,lapply(data.frame(t(r[,-1])),replace.na,999))
##  ID obs1 obs2 obs3 obs4 obs5
##1  A   NA    0    1  999  999
##2  B    1  999    2    3  999

这篇关于按行顺序识别NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆