Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R删除数据帧的第一行,直到第一行没有NA_R_Function_Dataframe_While Loop_Na - Fatal编程技术网

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

R删除数据帧的第一行,直到第一行没有NA,r,function,dataframe,while-loop,na,R,Function,Dataframe,While Loop,Na,我在数据框上应用na.approx,如果na恰好位于我的数据库的第一行或最后一行,这将不起作用 如何编写执行以下操作的函数: 当数据帧第一行的任何值为NA时,请删除第一行 示例数据帧: 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,] 我目前的尝试都与此相似: repl

我在数据框上应用na.approx,如果na恰好位于我的数据库的第一行或最后一行,这将不起作用

如何编写执行以下操作的函数: 当数据帧第一行的任何值为NA时,请删除第一行

示例数据帧:

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

您可以使用
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()

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

@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()

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.max
is.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

我们还可以使用
which.max
is.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

你赢了我5秒。。。哦,好的。谢谢你的解决方案,这将对许多其他用户非常有帮助,对我也很有用。我选择了另一个答案,因为它修复了我混乱的代码,并且更容易应用到我的第二步(对最后几行执行相同的操作)你比我快了5秒。。。哦,好的。谢谢你的解决方案,这将对许多其他用户非常有帮助,对我也很有用。我选择了另一个答案,因为它修复了我混乱的代码,并且更容易应用到我的第二步(对最后一行执行相同的操作)