Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R purrr映射在输出中显示列名_R_Purrr - Fatal编程技术网

R purrr映射在输出中显示列名

R 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

我试图跨向量输入运行purr
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))