Sometimes one may want to change names of columns in a table. Here is what might seem like a logical approach, but it is in fact difficult to read, awkward and repetitive, and slow.
new_names=list()
l=1
for(n in colnames(x)){
ifelse((l<10), (colnames(x)[l]=c(paste0('Otu000',c(l)))), (colnames(x)[l]=c(paste0('Otu00',c(l)))))
new_names=append(new_names, colnames(x)[l])
l=l+1
}
Here is an improved version:
new_names <- paste0("Otu", stringr::str_pad(seq_along(colnames(x)), 4, pad="0"))
It might be helpful to work up the full example, showing original code and improved version.