Alexander
Alexander

Reputation: 4635

Conditional filling NA rows with comparing non-NA labeled rows

I want to fill NA rows based on checking the differences between the closest non-NA labeled rows.

For instance

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

I want to evaluate how to change NA rows with checking the difference between sd_value and value those close to bad and unablerows.

if we want to get differences between the rows we can do;

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

The condition how I want to label the NA rows is

if the diff_val<50 and diff_sd_val<9 label them with the last non-NA label else use the first non-NA label after the last NA row.

So that the expected output would be

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

The possible solution I cooked up so far:

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

which gives

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

Upvotes: 0

Views: 43

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

One option is to find NA and non-NA index and based on the condition select the closest label to it.

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

You can remove diff_val and diff_sd_val columns later if not needed.


We can also create a function

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

and then apply it

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

If we want to apply it by group we can add a group_by statement and it should work.

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

Upvotes: 1

Related Questions