Home > Net >  Replacing NA Values in a data frame using a nested for loop and if, else if, else
Replacing NA Values in a data frame using a nested for loop and if, else if, else

Time:08-30

I have been trying to replace the NAs of the "URL" column, in the loop: "url_col", with something like "". So far I have this:

url_col <- which(colnames(data_mat) == "URL") 

for (c in 1:ncol(data_mat)) {
  for (r in 1:nrow(data_mat)) {
    if(c == lit_col) {            
      cell <-  flatten(data_mat[r,c]) 
    } else if (c == url_col & is.na(data_mat[[r]])){       
      cell <- gsub("NA", "", cell)                         
    } else {
      cell <- unlist(data_mat[r,c])
      cell <- paste(cell, collapse = ", ")
    }
   }
}

I have also tried:

#is.na(data_mat[[r,c]])) 
#is.na([r])

But I get the warning "the condition has length > 1" or the results are not what I expected

CodePudding user response:

If you want to only replace NA in the URL column

mi_df %>%  
  mutate(URL = URL %>% replace_na(""))

# A tibble: 5 x 5
  identifier description period literature URL   
       <int> <chr>        <dbl> <chr>      <chr> 
1          1 a              1.2 book1      ""    
2          2 b              3.4 book2      "web1"
3          3 c              4.5 NA         ""    
4          4 d              5.6 NA         "web2"
5          5 e             10   book3      ""    

But, if you want to replace all NA in the columns that contains characters, you can do

mi_df %>%  
  mutate(across(where(is.character), ~ replace_na(.x, "")))

# A tibble: 5 x 5
  identifier description period literature URL   
       <int> <chr>        <dbl> <chr>      <chr> 
1          1 a              1.2 "book1"    ""    
2          2 b              3.4 "book2"    "web1"
3          3 c              4.5 ""         ""    
4          4 d              5.6 ""         "web2"
5          5 e             10   "book3"    ""    

CodePudding user response:

The solution to eliminate the NA's within a nested for was to employ a nested if else

mi_df <- data.frame(
  "identifier" = 1:5, 
  "description" = c("a", "NA; ", "c", "d", "NA; "), 
  "period" = c(1.2, 3.4, 4.5, 5.6, NA),
  "literature" = as.character(c("book1", "book2", NA, NA, "book3")),
  "URL" = c(NA, "web1", NA, "web2",NA)
)

lit_c <- which(colnames(mi_df) == "literature") 
url_c <- which(colnames(mi_df) == "URL")

for (c in 1:ncol(mi_df)) {
  for (r in 1:nrow(mi_df)) {
    if(c == lit_c) {            
      #cell <-  flatten(mi_df[r,c]) 
    } else if (c == url_c){
      cell <- if (is.na(mi_df[r,c])) "" else mi_df[r,c]  #to replace NA with ""                        
    } else {
      cell <- unlist(mi_df[r,c])
      cell <- gsub("NA; ", "xy", cell)  #gsub() used for replacement operations
    }
    mi_df[r,c] <- (cell)
  }
}
  • Related