I know that this question has been asked before but I can not get it to work for me and I swear I tried many ways do do it from for file in loops to lapply. I have tables in which I want to replace the columns 2 to 7 'S headers which are now in this format: "X1","X2","X3","X4","X5","X6","X7" into "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species".
Each table does not have the same amount of row nor column.
My 31 tables are listed as this:
step4 <- list.files(pattern="*.coldrop.tsv")
Also, and this is a ''sub-problem'', I am doing it from the 2nd column because RAM keeps adding row numbers (1,2,3,4,5,6....n). If anyone can help me here that would be great.. I need to do it on all these ''step4'' list of tables. here are some ''samples'' of what I want to do.
when I fisrt was trying I opted for the for file in loop option:
colnames <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
The following works on a single file
names(Omlo_run11_table.tsv.step1.tsv.step2.tsv.step3.tsv.coldrop.tsv)[2:8] <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
i = 1
for(i in 1:length(step4)){
names(step4[i])[2:8] <- c("Kingdom","Phylum","Class","Order","Family","Genus","Species")
}
I get this: Error in names(step4[i])[2:8] <- c("Kingdom", "Phylum", "Class", "Order", : 'names' attribute [8] must be the same length as the vector [1]
names(get(step4[i]))[names(get(step4[i])) == "X1","X2","X3","X4","X5","X6","X7"] <- c("Kingdom","Phylum","Class","Order","Family","Genus","Species")
I get this: Error in names(get(step4[i]))[names(get(step4[i])) == "X1", "X2", "X3", : incorrect number of subscripts
for(i in 1:length(step4)){
nm <- paste0("step4[i]")
tmp <- get(nm)
colnames(tmp)[2:8] <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
assign(nm, tmp)
}
I get this: Error in get(nm) : object 'step4[i]' not found
lapply (step4, function(df) { colnames(df)[2:length(step4)] <-colnames[1:length(step4)]-1)}
and so on... I am more of a for file in type of person but I am open to lapply options. I encountered solutions with setnames but could not figure it out either.. Can please someone help me...