title: How can I add NA values to empty rows within a loop?
body: <p>I am tasked with creating a loop that does a few things: </p>
  
  <ol>
  <li>Eliminates rows with missing data for the "height" variable</li>
  <li>Selects the first visit available (already ordered beforehand)</li>
  <li>Appends the remaining value for "height" for a partcular patient in a dataframe</li>
  </ol>
  
  <p>Essentially, the goal is to obtain the first non-missing height value from a set of observations within each patient. Every patient has different amounts of observations and "missingness" so I was advised to create a loop for this task</p>
  
  <p>This is my attempt:</p>
  
  <pre><code>for (i in 1:length(patients)){
  
  # select data for one patient
  patient = patients[i]
  SubjectID_patient = filter(DF, Subject.ID == patient)
  
  #select data which is not missing
  complete_hts=filter(SubjectID_patient,
  !is.na(SubjectID_patient$height_cm))
  
  #select first visit for height
  complete_hts=complete_hts[!duplicated(complete_hts$Subject.ID),]
  
  # if there are 0 entries remaining, return NAs
  ????
  
  #make dataframe for the patient
  df_patient = data.frame(Subject.ID = complete_hts$Subject.ID,
                          height =  complete_hts$height_cm)
  
  ##bind rows of this dataframe to first dataframe
  baseline_new = rbind(baseline_new, df_patient)
  
  }
  </code></pre>
  
  <p>So as you can see, I'm  stuck at the part where if there are 0 entries for a particular patient for the "height" variable, return NAs. The loop works fine until I encounter a visit where there are 0 valid values and because of this I cannot create a dataframe in the end. Any suggestions will be greatly appreciated. Please pardon my improper formatting (if any) as this is my first post on stackoverflow!</p>
  
  <p>Thanks!</p>