如何删除一列中超过2个连续的NA? [英] How to remove more than 2 consecutive NA's in a column?
本文介绍了如何删除一列中超过2个连续的NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是R的新手,在我的数据框中有col1("Timestamp"),col2("Values").我必须删除col2中超过2个连续NA的行.我的数据框如下图所示,
I am new to R, In my data Frame I have col1("Timestamp"), col2("Values"). I have to remove rows of more than 2 consecutive NA in col2. My dataframe Looks like the below one,
Timestamp | values
-----------|--------
2011-01-02 | 2
2011-01-03 | 3
2011-01-04 | NA
2011-01-05 | 1
2011-01-06 | NA
2011-01-07 | NA
2011-01-08 | 8
2011-01-09 | 6
2011-01-10 | NA
2011-01-11 | NA
2011-01-12 | NA
2011-01-13 | 2
我想根据第二列删除2条以上的重复行.预期输出-
I would like to remove more than 2 duplicate rows based on second column. Expected output -
Timestamp | values
-----------|--------
2011-01-02 | 2
2011-01-03 | 3
2011-01-04 | NA
2011-01-05 | 1
2011-01-06 | NA
2011-01-07 | NA
2011-01-08 | 8
2011-01-09 | 6
2011-01-13 | 2
我正在寻找解决方案,谢谢.
I'm looking for the solution thanks in advance.
推荐答案
您可以使用行程编码功能rle
.我认为数据已经按日期排序了.
You can use the run length encoding function rle
. I assume that the data is already sorted by date.
r <- rle(is.na(df$values)) # check runs of NA in value column
df[!rep(r$values & r$lengths > 2, r$lengths),] # remove runs of >2 length
这篇关于如何删除一列中超过2个连续的NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文