First time question asker here!
I am working with a script that imports multiple data frames and before passing them to an function that performs an analysis, it creates a vector for each data frame containing all the column names. The script does a check to make sure all of the column names across all the vectors match, if they do, the analysis goes ahead, if not it aborts. Unfortunately my 25 different data frames don’t have matching columns, so my question:
How can I loop over all the vectors, remove the colnames that don’t match and then do the same for the imported data frames. I have created a simplified example below as well as a some of the solutions I have tried.
Many thanks for the help
## goal
# example vectors:
cols_import_1 <- c('a', 'b', 'c', 'd', 'f')
cols_import_2 <- c('a', 'b', 'c', 'aa', 'cc', 'f')
cols_import_3 <- c('a', 'b', 'c', 'bb', 'e', 'f')
# what needs to be produced:
cols_import_1 <- c('a', 'b', 'c', 'f')
cols_import_2 <- c('a', 'b', 'c', 'f')
cols_import_3 <- c('a', 'b', 'c', 'f')
## my approach
# create list of loci to remove from vector
remove_list <- c('aa'
, 'bb'
, 'cc'
, 'd'
, 'e'
)
# create vector list
vector_list <- list(cols_import_1
, cols_import_2
, cols_import_3)
# option 1:
lapply(vector_list, function(change_vector){
change_vector <- change_vector[! change_vector %in% remove_loci_list]
})
# option 2:
for(i in 1:length(vector_list)){
vector_list[[i]] <- vector_list[[i]][!vector_list[[i]] %in% remove_loci_list]
}
# option 3:
for(i in 1:length(paste0('cols_import_', [i]))){
cols_import_[i] <- cols_import_[i][!cols_import_[i] %in% remove_loci_list]
}
# option 4:
for(i in 1:length(paste0(cols_import_, [i]))){
paste0(cols_import_, [i]) <- paste0(cols_import_, [i])[!paste0(cols_import_, [i]) %in% remove_loci_list]
}
# option 5:
for(i in 1:length(vector_list){
cols_import_[i] <- cols_import_[i][!cols_import_[i] %in% remove_loci_list]
}
user12340446 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.