R 删除第一行数据框,直到第一行没有 NA

R remove first row of data frame until first row has no NA

我正在对一个数据框应用 na.approx,如果 NA 恰好在我的数据库的第一行或最后一行,这将不起作用。

如何编写函数来执行以下操作: "While any value of the first row of the data frame is NA, remove the first row"

示例数据框:

x1=x2=c(1,2,3,4,5,6,7,8,9,10,11,12)
x3=x4=c(NA,NA,3,4,5,6,NA,NA,NA,NA,11,12)
df=data.frame(x1,x2,x3,x4)

此示例数据框的结果应如下所示:

result=df[-1:-2,]

我目前的尝试都与此类似:

replace_na=function(df){
while(anyNA(df[1,])=TRUE){
  df=df[-1,],
  return(df)
}  
#this is where I would apply the na.approx function to the data frame
}

任何帮助将不胜感激,谢谢!

您可以使用complete.cases。使用 cumsum,将删除第一个不完整的行:

df[cumsum(complete.cases(df)) != 0, ]
   x1 x2 x3 x4
3   3  3  3  3
4   4  4  4  4
5   5  5  5  5
6   6  6  6  6
7   7  7 NA NA
8   8  8 NA NA
9   9  9 NA NA
10 10 10 NA NA
11 11 11 11 11
12 12 12 12 12

@Psidom 的回答很好,但你也可以修复自己的自定义函数:

replace_na=function(df){
   while(anyNA(df[1,])==TRUE){
      df=df[-1,]
   }
#this is where I would apply the na.approx function to the data frame
return(df)
}

在第二行,== 是您需要使用的等号。在第二行,逗号是多余的。最后,return() 需要移出 while 循环。

replace_na(df)
#    x1 x2 x3 x4
# 3   3  3  3  3
# 4   4  4  4  4
# 5   5  5  5  5
# 6   6  6  6  6
# 7   7  7 NA NA
# 8   8  8 NA NA
# 9   9  9 NA NA
# 10 10 10 NA NA
# 11 11 11 11 11
# 12 12 12 12 12

我们也可以使用which.maxis.na

df[which.max(!rowSums(is.na(df))):nrow(df),]
#   x1 x2 x3 x4
#3   3  3  3  3
#4   4  4  4  4
#5   5  5  5  5
#6   6  6  6  6
#7   7  7 NA NA
#8   8  8 NA NA
#9   9  9 NA NA
#10 10 10 NA NA
#11 11 11 11 11
#12 12 12 12 12