R purrr映射在输出中显示列名
我试图跨向量输入运行purrR purrr映射在输出中显示列名,r,purrr,R,Purrr,我试图跨向量输入运行purrmap,在输出中,我希望输出列具有有意义的名称 x <- c("a", "b") y <- "end." map_dfc(x, function(x) paste("pre ", x, y)) names(x) <- x map_dfc(x, function(x) paste(x, y)) 有没有办法避免跑步的需要 names(x) <- x names(x)一个简单的方法是命名输入元素。这通常会导致代码更加一致 library(pu
map
,在输出中,我希望输出列具有有意义的名称
x <- c("a", "b")
y <- "end."
map_dfc(x, function(x) paste("pre ", x, y))
names(x) <- x
map_dfc(x, function(x) paste(x, y))
有没有办法避免跑步的需要
names(x) <- x
names(x)一个简单的方法是命名输入元素。这通常会导致代码更加一致
library(purrr)
x <- setNames(c("a", "b"), nm = c("a", "b"))
# x <- setNames(nm = c("a", "b")) # this is short cut of above
y <- "end."
map_dfc(x, function(x) paste("pre ", x, y))
库(purrr)
x除了@Ronaksah建议setNames()
的评论外,您可以在不使用purrr::map()的情况下处理此问题:
paste("pre ", x, y) %>%
as.list() %>%
as.data.frame(col.names = x, stringsAsFactors = F)
purr
方法是使用注释中的set_names
(尽管purr
中正确的setNames
似乎被set_names
替换):
使用set_names
时,如果使用默认参数,则无需指定新名称。如何set_names
<代码>设置名称(映射dfc(x,函数(x)粘贴(“pre”,x,y)),x)
?
library(purrr)
x <- setNames(c("a", "b"), nm = c("a", "b"))
# x <- setNames(nm = c("a", "b")) # this is short cut of above
y <- "end."
map_dfc(x, function(x) paste("pre ", x, y))
paste("pre ", x, y) %>%
as.list() %>%
as.data.frame(col.names = x, stringsAsFactors = F)
map_dfc(set_names(x), function(x) paste("pre ", x, y))