I have list of dataframes (List of 4) in global environment for match data called match_list. I wanted to transpose the list with match_list <- lapply(match_list,t)
so it makes metrics as colnames.
It transposed the dataframe in each list but it also made previous colnames as rownames. Could you please helped me how to get rid off the rownames? And make it as a standard column?
I am then performing for loop
#### create individual dataframes ----
for (i in seq(match_list))
assign(paste0(file_names[[i]]), match_list[[i]])
that creates separate dataframes in my global environment
I have tried several ways, but still cannot turn my head around to make it work.
My attempt:
match_list <- lapply(match_list,function(x){
colnames(x) <- rownames_to_column(match_list[[1]])
})
This is my whole script:
match_xlsx <- as_tibble(list.files("C:/Users/User/Desktop/asJohan", pattern = ".xlsx", full.names = TRUE, all.files = FALSE))
file_names <- list.files("C:/Users/User/Desktop/asJohan", pattern = ".xlsx", full.names = TRUE, all.files = FALSE)
#### read specific sheet from excel ---- create two lists and merge them
match_list <- lapply(match_xlsx$value, read_excel, sheet = 1, range = "B1:N40")
match_list <- lapply(match_list,t)
#match_list <- lapply(match_list, function(x) {x <- x[-1,-1 ]}) #### remove first column and row
###set first row as a header - column names
match_list <- lapply(match_list, function(x){
colnames(x) <- x[1,]
x[-(1:3),]})
####set rowname as column name
match_list <- lapply(match_list, rownames_to_column, var='former_transposed_colnames')
#### create individual dataframes ----
for (i in seq(match_list))
assign(paste0(file_names[[i]]), match_list[[i]])```