【发布时间】:2020-10-28 07:03:34
【问题描述】:
我想将数据框名称附加到以下数据框列表中的每一列:
df1 <- tibble(V1=c(1, 1, 3, 1),
V2=c(2, 1, 2, 2),
V3=c(4, 1, 1, 2))
df2 <- tibble(V1=c(1, 1, 3, 1),
V2=c(2, 1, 2, 2),
V3=c(4, 1, 1, 2))
df <- list(df1, df2)
names(df) <- c("df1", "df2")
df
这就是我想要的样子:
$df1
# A tibble: 4 x 3
df1_V1 df1_V2 df1_V3
<dbl> <dbl> <dbl>
1 1 2 4
2 1 1 1
3 3 2 1
4 1 2 2
$df2
# A tibble: 4 x 3
df2_V1 df2_V2 df2_V3
<dbl> <dbl> <dbl>
1 1 2 4
2 1 1 1
3 3 2 1
4 1 2 2
我试过了:
appendDFnameToColumns <- function(df, prefix = "", sep = "") {
colnames(df) <- paste(prefix, colnames(df), sep = sep)
df
}
df <- map(df, appendDFnameToColumns,
prefix = names(df$.),
sep ="_")
提前致谢
【问题讨论】:
标签: r