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

时间:2019-04-24 01:58:20

标签: r function dplyr difference

我想基于检查最接近的未标记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

我想通过检查NAsd_value行附近的valuebad之间的差异来评估如何更改unable行。

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

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标签标记它们,否则使用最后non-NA行之后的第一个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))
  

mutate_impl(.data,点)中的错误:     评估错误:缺少值,需要TRUE / FALSE。   另外:警告消息:   如果if(is.na(label [i])&diff_sd_val <9&diff_val <50){:     条件的长度> 1,并且只会使用第一个元素

1 个答案:

答案 0 :(得分:1)

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

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