I would like to rename columns sequentially, for multiple dataframes with varying number of columns.
The dataframes will be put into R from a pdf that displays a table, and each pdf page is automatically assigned to a column. Automatically, the columns are named as the entire printout of the page. I simply want to replace this automatic column name with the page number.
A dataframe with 4 columns titled 1,2,3,4 or a dataframe with 5 columns titles 1,2,3,4,5 and so on.
I tried
txt_df1 <- data.frame("page1", "page2", "page3", "page4")
#remember the dataframes might have any number of columns
for (n in (1:ncol(txt_df1))){
colnames(txt_df1[n]) <- n
}
and
txt_df1 <- data.frame("page1", "page2", "page3", "page4")
#remember the dataframes might have any number of columns
for (n in (1:ncol(txt_df1))){
txt_df1 <- rename(txt_df1, n = n)
}
For some reason, nothing happens when either of these are run. Any suggestions on how to do this better/ make this code work?