R/replace_row_na.R

Defines functions replace_row_na

## Requirement: 'tibble'

# Info: Takes a list of new informations and automatically replace corresponding NA in a dataframe.

# Note: The list must be named with the value in the reference column corresponding to the row(s) to missing informations.
# Note: It is mandatory to provide the same number of new infos than the number of NA in a row (provide NA if no replacement needed).

replace_row_na = function(data, new_infos, reference_col, deduplified_list = T){
  
  if(!all(names(new_infos) %in% data[[reference_col]])) stop("All index elements of the list are not in the reference column of the data.")
  
  if(deduplified_list) na_row = consensus_deduplification(data[rowSums(is.na(data)) > 0, ], reference_col)
  
  else if(!deduplified_list) na_row = data[rowSums(is.na(data)) > 0, ]
  
  na_row[na_row == "NA"] = NA
  
  if(nrow(na_row) == length(new_infos) && length(which(is.na(na_row))) != sum(lengths(new_infos))) 
    stop("Number of replacements and number of NA per unique value in the reference column are different.")
  
  data_replaced = data
  
  data_replaced[data_replaced == "NA"] = NA
  
  data_pos = which(data[[reference_col]] %in% names(new_infos))
  
  for(i in 1:length(data_pos)){
    
    data_replaced[data_pos[i], ] = replace(data_replaced[data_pos[i], ], which(is.na(data_replaced[data_pos[i], ])), 
                                           lapply(new_infos[[data[rowSums(is.na(data)) > 0, ][i, ][[reference_col]]]], function(x) list(x)[[1]]))
    
  }
  
  tibble(data_replaced)
  
}
Eliot-RUIZ/eDNAevaluation documentation built on Dec. 17, 2021, 6:25 p.m.