我有一个数据集,我想使用相同长度的字符列表重命名所有列。
这是我目前的代码,尝试使用该rename
命令。
eurrefcolnames = c('euRefVoteW1','euRefVoteW2', 'euRefVoteW3', 'euRefVoteW4', 'euRefVoteW6','euRefVoteW7', 'euRefVoteW8', 'euRefVoteW9')
times <- in_all_waves %>%
filter(Paper == 'Times') %>%
ungroup() %>%
select(-Paper) %>%
map_df(table) %>%
t %>%
as_tibble() %>%
rename(eurrefcolnames = names(times))
但这似乎不起作用。该函数replace_with
需要一个函数,不得不编写一个仅调用列表的函数似乎很愚蠢。有没有更简单的方法我看不到?
这是我用于上下文的表(在之后as_tibble
):
structure(list(V1 = c(316L, 157L, 2L, 56L), V2 = c(290L, 123L,
3L, 51L), V3 = c(313L, 159L, 3L, 55L), V4 = c(324L, 154L, 3L,
50L), V5 = c(338L, 134L, 2L, 57L), V6 = c(320L, 189L, 2L, 20L
), V7 = c(325L, 187L, 2L, 17L), V8 = c(335L, 181L, 0L, 0L)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
您可以set_names
从purrr
包装中使用:
mydf %>%
purrr::set_names(eurrefcolnames)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句