【发布时间】:2021-02-25 13:38:10
【问题描述】:
我在全局环境中有名为 match_list 的匹配数据的数据框列表(列表 4)。我想用match_list <- lapply(match_list,t) 转置列表,以便将指标作为列名。
它转置了每个列表中的数据框,但它也将以前的列名作为行名。你能帮我摆脱行名吗?并使其成为标准列?
然后我正在执行 for 循环
#### create individual dataframes ----
for (i in seq(match_list))
assign(paste0(file_names[[i]]), match_list[[i]])
在我的全局环境中创建单独的数据框
我尝试了几种方法,但仍然无法转过头来使其发挥作用。
我的尝试:
match_list <- lapply(match_list,function(x){
colnames(x) <- rownames_to_column(match_list[[1]])
})
这是我的整个脚本:
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]])```
【问题讨论】: