lundi 29 juillet 2019

Is there a way to fill in NA based on a single entry in one column and matches from another?

I am trying to fill in a column for a dataframe I am working with. As of now, each row is a sample, and multiple samples may be assigned the same individual ID. If this is the case, only the first row with the ID contains the sex type, the remaining rows are NA. For the analysis I am doing the sex needs to be filled in for everything.

I initially fixed this by just exporting it to excel and copying in the sex designation by hand, but this is tedious and I am going to have to do this over many iterations. I attempted to use for and if-else loops to accomplish this. While the loop did not give any errors, it also didn't change any of the sex designations.

deerid <- c(1,2,2,2,3,4,5,5,6,7,8,8,9,9,9)
deersx <- c("m", "f", NA, NA, "f", "m", "m", NA, "f", "f", "f", NA, "m", NA, NA)
samp2 <- as.data.frame(cbind(deerid, deersx))

for (i in 1:length(samp2)) {
  for (j in 1:length(samp2)) {
    if (samp2$deerid[i] == samp2$deerid[j]) {
      if (is.na(samp2$deersx[i]) == FALSE) {
        samp2$deersx[j] <- samp2$deersx[i]
      } else {
        if (is.na(samp2$deersx[j]) == FALSE) {
          samp2$deersx[i] <- samp2$deersx[j]
        } else {
          NA
        }
      }
     } else {
      NA
    }

This didn't change the sex column at all. I was hoping that the NA values in the deersx column would be changed to m or f, such that it would actually read: m, f, f, f, f, m, m, m, f, f, f, f, m, m

Thank you for any thoughts and suggestions.

Aucun commentaire:

Enregistrer un commentaire