R/replace_manual_taxo_infos.R

Defines functions replace_manual_taxo_infos

## Requirement: 'tibble'

# Info: This function add the new taxa names (from manual searching) in the dataframe 
# Info: according to the previous ranks returned by get_taxa_missing_1_infos().

replace_manual_taxo_infos = function(data, old_infos, new_infos){
  
  data_replaced = data
  
  if(any(names(old_infos) != names(new_infos))) 
    stop("Both lists must have the same index names and they must be the names of the columns with missing taxonomic infos in the data.")
  
  if(any(lengths(old_infos) != lengths(new_infos))) stop(paste("Not the same number of elements between the old and the new data_replaced in:", 
                                                               paste0(names(which(lengths(old_infos) != lengths(new_infos))), collapse = ", "), 
                                                               "columns"))
  
  if(length(which(data == "Incertae sedis")) != 0 && any(lengths(lapply(new_infos, function(x) x[grepl("Incertae sedis", x)])) != 0))
    warning('There is already "Incertae sedis" taxa in the original dataframe.')
  
  if(length(which(data == "Not assigned")) != 0 && any(lengths(lapply(new_infos, function(x) x[grepl("Not assigned", x)])) != 0))
    warning('There is already "Not assigned" taxa in the original dataframe.')
  
  for(i in 1:length(old_infos)){
    
    previous_name = names(data_replaced[, which(colnames(data_replaced) == names(old_infos)[i]) - 1])
    
    for(j in 1:lengths(old_infos)[i]){
      
      data_replaced[[names(old_infos)[i]]] = replace(data_replaced[[names(old_infos)[i]]], 
                                                     which(data_replaced[[previous_name]] == old_infos[i][[1]][j]), 
                                                     new_infos[i][[1]][j])
      
    }
    
  }
  
  tibble(data_replaced)
  
}
Eliot-RUIZ/eDNAevaluation documentation built on Dec. 17, 2021, 6:25 p.m.