簡體   English   中英

如何在某些行的序列中定義沒有 NA 的列?

[英]How to define column that doesn't have NAs in sequence of certain rows?

以下是一些示例數據:

    df1 <- read.table(text = "Date  Client1 Client2 Client3
                  01.01.2019    0   0   2
                  01.02.2019    0   0   3
                  01.03.2019    0   0   4
                  01.04.2019    0   0   4
                  01.05.2019    0   0   4
                  01.06.2019    1   0   4
                  01.07.2019    0   0   0
                  01.08.2019    0   0   1
                  01.09.2019    0   0   0
                  01.10.2019    0   3   0
                  01.11.2019    0   0   2
                  01.12.2019    2   0   0
                  01.01.2020    3   4   3
                  01.02.2020    4   0   3
                  01.03.2020    5   0   0
                  01.04.2020    5   0   0
                  ", header = TRUE)
df1[df1 == 0] <- NA

問題是如何為包含不包含 NA 的 5 行或更多行的序列的每一列找到邏輯索引。

Client1 TRUE
Client2 FALSE
Client3 TRUE

我會使用rle()函數來計算!is.na()運行長度。 例如,使用您對df1的定義:

df2 <- data.frame(Name = character(3), Group = character(3))

for (i in 1:3) {
  runs <- rle(!is.na(df1[, i + 1]))
  good <- which(runs$values == TRUE)
  runs <- runs$lengths
  n <- length(runs)
  df2$Group[i] <- if (n %in% good) "Stable"
                  else if (max(runs[good]) >= 5) "Was_Stable"
                  else "Not_Stable"
  df2$Name[i] <- names(df1)[i + 1]
}

您可以使用sapply迭代列並檢查any

sapply(df1[-1], function(x) any(with(rle(!is.na(x)), values & lengths >= 5)))

# Client1 Client2 Client3 
#   TRUE   FALSE    TRUE 

類似user2554330 ,那么你可以使用rle是這樣的:

# add a column with no NAs as an example
df1 <- cbind(df1, dummy = 1:NROW(df1)) 

# find columns with five or more NAs in a row
is_num <- vapply(df1, is.numeric, TRUE) # assume we only look at numerics?
res <- setNames(rep(TRUE, NCOL(df1)), colnames(df1))
res[is_num] <- vapply(df1[is_num], function(x){
  o <- rle(!is.na(x))
  any(o$lengths[o$values] > 4)
}, TRUE)
res
#R> Date Client1 Client2 Client3   dummy 
#R> TRUE    TRUE   FALSE    TRUE    TRUE

我希望這會很快。 如果您不關心其他列,則可以執行以下操作:

is_num <- vapply(df1, is.numeric, TRUE)
vapply(df1[is_num], function(x){
  o <- rle(!is.na(x))
  any(o$lengths[o$values] > 4)
}, TRUE)
#R> Client1 Client2 Client3 
#R>    TRUE   FALSE    TRUE

事后我意識到這只是對Ronak Shah回答的一個小改動 將他的方法與我的解決方案相結合,結果如下:

vapply(df1[vapply(df1, is.numeric, TRUE)], function(x)
  with(rle(!is.na(x)), any(lengths[values] > 4)), TRUE)
#R> Client1 Client2 Client3 
#R>    TRUE   FALSE    TRUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM