0

My data looks like below.

  ID       Group timing glucose_level
   <chr>    <dbl>  <int>         <dbl>
 1 black 7      0      0           136
 2 black 1      0      0           116
 3 blue 20      0      0           144
 4 green 18     0      0           114
 5 red 4        0      0           126
 6 red 5        0      0            80
 7 green 17     0      0           111
 8 green 3      0      0           109
 9 red 20       0      0            96
10 black 39     0      0           140

There are some missing values in glucose level. Below are part of glucose level data

[697] 128 157 132 142 141 128  97 120 123 131 132 126 140 103 147 181 217 257 218 234 240 281 273 224 210 227  NA  NA 245
[726] 230 252 270 238 134 173 193 151 128 180 218 218 190 225 214 186 140 237 239 279 246 244 146 196 157 178 140 127 187
[755] 206 177 220 179 167 127 219 223 241 162 235 140 187 154 172 116 139 194 173 150 187 131 176 114 154 180 223 150 219
[784] 130 169 104 136 132 121 175 169 128 110 101 100  92 122 196 203  96 143 129  NA  72 141 143 129 149 132 107  94  76
[813]  80  95  63 198 181  86 122

I wanna use a loop to replace the missing values. Here are my code:

    for(i in 1:length(data)){
    if(is.na(data[i,'glucose_level'])){
      if(data[i,'Group']==0){
        data[i,'glucose_level']=162.7059
      }else if(data[i,'Group']==1){
        data[i,'glucose_level']= 163.1415
      }else{
        data[i,'glucose_level']= 165.9106
      }
}
}

I print out data$glucose_level and find there are still missing values in it.why no changes in my data???

melody
  • 1
  • 1
  • You are checking if the data is NA (missing) before you change any of the values. Since you don't have any NA values, that `if()` statement is always false, you probably meant to find non missing values. use `!is.na()` instead – MrFlick Oct 06 '20 at 04:21
  • Hi, MrFlick, There are missing values in glucose level. – melody Oct 06 '20 at 04:24
  • I don't see any in the data you posted. Make sure to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that we can copy/paste into R for testing. – MrFlick Oct 06 '20 at 04:24
  • 1
    Also you are doing `1:length(data)` which almost certainly not right. You probably meant `1:nrow(data)` – MrFlick Oct 06 '20 at 04:25
  • Yes. you are right. It works now. – melody Oct 06 '20 at 04:34

2 Answers2

1

You can use nested ifelse or case_when and check for conditions and assign values accordingly.

library(dplyr)

data <- data %>%
          mutate(glucose_level = case_when(!is.na(glucose_level) ~ glucose_level,
                                             Group == 0  ~ 162.7059, 
                                             Group == 1 ~ 163.1415, 
                                             TRUE ~ 165.9106))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can use fcase from data.table

library(data.table)
setDT(data)[, glucose_level := fcase(!is.na(glucose_level), glucose_level,
                                         Group == 0, 162.7059, 
                                         Group == 1,163.1415, 
                                         165.9106)]
akrun
  • 874,273
  • 37
  • 540
  • 662