使用 by_row(., collate = "col") 时防止重命名列
Prevent columns from being renamed when using by_row(., collate = "col")
代码
library(purrrlyr)
mtcars[1:2, 1:2] %>%
by_row(function(x)
as.data.frame(setNames(as.list(1:5), LETTERS[1:5])),
.collate = "cols")
# # tibble [2 x 7]
# mpg cyl A1 B1 C1 D1 E1
# <dbl> <dbl> <int> <int> <int> <int> <int>
# 1 21 6 1 2 3 4 5
# 2 21 6 1 2 3 4 5
问题
有没有办法通过在列名称中添加 1
来避免 by_row
"decorates" 我的数据框的名称?
预期结果
# # tibble [2 x 7]
# mpg cyl A B C D E
# <dbl> <dbl> <int> <int> <int> <int> <int>
# 1 21 6 1 2 3 4 5
# 2 21 6 1 2 3 4 5
如评论中所述,此方法会生成所需的列,但会添加 .row
列。
library(purrrlyr)
mtcars[1:2, 1:2] %>%
by_row(function(x)
as.data.frame(setNames(as.list(1:5), LETTERS[1:5])),
.collate = "rows")
# mpg cyl .row A B C D E
# <dbl> <dbl> <int> <int> <int> <int> <int> <int>
# 1 21 6 1 1 2 3 4 5
# 2 21 6 2 1 2 3 4 5
代码
library(purrrlyr)
mtcars[1:2, 1:2] %>%
by_row(function(x)
as.data.frame(setNames(as.list(1:5), LETTERS[1:5])),
.collate = "cols")
# # tibble [2 x 7]
# mpg cyl A1 B1 C1 D1 E1
# <dbl> <dbl> <int> <int> <int> <int> <int>
# 1 21 6 1 2 3 4 5
# 2 21 6 1 2 3 4 5
问题
有没有办法通过在列名称中添加 1
来避免 by_row
"decorates" 我的数据框的名称?
预期结果
# # tibble [2 x 7]
# mpg cyl A B C D E
# <dbl> <dbl> <int> <int> <int> <int> <int>
# 1 21 6 1 2 3 4 5
# 2 21 6 1 2 3 4 5
如评论中所述,此方法会生成所需的列,但会添加 .row
列。
library(purrrlyr)
mtcars[1:2, 1:2] %>%
by_row(function(x)
as.data.frame(setNames(as.list(1:5), LETTERS[1:5])),
.collate = "rows")
# mpg cyl .row A B C D E
# <dbl> <dbl> <int> <int> <int> <int> <int> <int>
# 1 21 6 1 1 2 3 4 5
# 2 21 6 2 1 2 3 4 5