有条件地填充 NA 行并比较非 NA 标记的行

Conditional filling NA rows with comparing non-NA labeled rows

我想根据检查最接近 非 NA 标记行之间的差异来填充 NA 行。

例如

data <- data.frame(sd_value=c(34,33,34,37,36,45),  
                   value=c(383,428,437,455,508,509),                   
                   label=c(c("bad",rep(NA,4),"unable")))

> data
  sd_value value  label
1       34   383    bad
2       33   428   <NA>
3       34   437   <NA>
4       37   455   <NA>
5       36   508   <NA>
6       45   509 unable

我想评估如何更改 NA 行,检查靠近 badunable 行的 sd_valuevalue 之间的差异。

如果我们想得到我们可以做的行之间的差异;

library(dplyr)
data%>%
mutate(diff_val=c(0,diff(value)), diff_sd_val=c(0,diff(sd_value)))

  sd_value value  label diff_val diff_sd_val
1       34   383    bad        0           0
2       33   428   <NA>       45          -1
3       34   437   <NA>        9           1
4       37   455   <NA>       18           3
5       36   508   <NA>       53          -1
6       45   509 unable        1           9

我想要标记 NA 行的条件是

如果 diff_val<50diff_sd_val<9 使用最后一个 non-NA 标签标记它们,否则使用最后一个 NA 行之后的第一个 non-NA 标签。

这样预期的输出就是

sd_value value  label diff_val diff_sd_val
    1       34   383    bad        0           0
    2       33   428    bad       45          -1
    3       34   437    bad        9           1
    4       37   455    bad       18           3
    5       36   508 unable    53          -1
    6       45   509 unable        1           9

目前我想出的可能解决方案:

custom_labelling <- function(x,y,label){

    diff_sd_val<-c(NA,diff(x))

    diff_val<-c(NA,diff(y))
    label <- NA
    for (i in 1:length(label)){

      if(is.na(label[i])&diff_sd_val<9&diff_val<50){

      label[i] <- label
      }
      else {

        label <- label[i]
      }
    }
    return(label)
  }

这给出了

data%>%
  mutate(diff_val=c(0,diff(value)), diff_sd_val=c(0,diff(sd_value)))%>%
  mutate(custom_label=custom_labelling(sd_value,value,label))

Error in mutate_impl(.data, dots) : Evaluation error: missing value where TRUE/FALSE needed. In addition: Warning message: In if (is.na(label[i]) & diff_sd_val < 9 & diff_val < 50) { : the condition has length > 1 and only the first element will be used

一种选择是查找 NA 和非 NA 索引,并根据条件 select 找到最接近它的标签。

library(dplyr)

#Create a new dataframe with diff_val and diff_sd_val
data1 <- data%>% mutate(diff_val=c(0,diff(value)), diff_sd_val=c(0,diff(sd_value)))

#Get the NA indices
NA_inds <- which(is.na(data1$label))
#Get the non-NA indices
non_NA_inds <- setdiff(1:nrow(data1), NA_inds)

#For every NA index
for (i in NA_inds) {
   #Check the condition
   if(data1$diff_sd_val[i] < 9 & data1$diff_val[i] < 50) 
     #Get the last non-NA label
     data1$label[i] <- data1$label[non_NA_inds[which.max(i > non_NA_inds)]]
   else
     #Get the first non-NA label after last NA value
     data1$label[i] <- data1$label[non_NA_inds[i < non_NA_inds]]
}


data1
#  sd_value value  label diff_val diff_sd_val
#1       34   383    bad        0           0
#2       33   428    bad       45          -1
#3       34   437    bad        9           1
#4       37   455    bad       18           3
#5       36   508 unable       53          -1
#6       45   509 unable        1           9

如果不需要,您可以稍后删除 diff_valdiff_sd_val 列。


我们也可以创建一个函数

custom_label <- function(label, diff_val, diff_sd_val) {
   NA_inds <- which(is.na(label))
   non_NA_inds <- setdiff(1:length(label), NA_inds)
   new_label = label

   for (i in NA_inds) {
     if(diff_sd_val[i] < 9 & diff_val[i] < 50) 
       new_label[i] <- label[non_NA_inds[which.max(i > non_NA_inds)]]
     else
       new_label[i] <- label[non_NA_inds[i < non_NA_inds]]
   }
  return(new_label)
 }

然后应用它

data%>% 
  mutate(diff_val = c(0, diff(value)), 
         diff_sd_val = c(0, diff(sd_value)), 
         new_label = custom_label(label, diff_val, diff_sd_val))


#  sd_value value  label diff_val diff_sd_val new_label
#1       34   383    bad        0           0       bad
#2       33   428   <NA>       45          -1       bad
#3       34   437   <NA>        9           1       bad
#4       37   455   <NA>       18           3       bad
#5       36   508   <NA>       53          -1    unable
#6       45   509 unable        1           9    unable

如果我们想按组应用它,我们可以添加一个 group_by 语句,它应该可以工作。

data%>% 
   group_by(group) %>%
   mutate(diff_val = c(0, diff(value)), 
          diff_sd_val = c(0, diff(sd_value)), 
          new_label = custom_label(label, diff_val, diff_sd_val))