使用地图从小标题中生成小标题列表,然后选择 [英] Generate a list of tibble from a tibble by using map and select
本文介绍了使用地图从小标题中生成小标题列表,然后选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用以下代码生成一个小菜一碟的列表。
I want to generate list of tibble fron one tibble in the following codes.
tbl = tibble(id=1:10, a = rnorm(10), b = rnorm(10))
tbl_list = c("a", "b") %>% map(~ tbl %>% select(c("id", .)))
我想要的输出是
tbl_list
[[1]]
# A tibble: 2 x 2
id a
<int> <dbl>
1 1 -0.704
2 2 -0.917
[[2]]
# A tibble: 2 x 2
id a
<int> <dbl>
1 1 -0.704
2 2 -0.917
但是,它显示了错误消息
However, it shows the error message,
c( id,。)
必须计算为列位置或名称,而不是list,
"c("id", .)
must evaluate to column positions or names, not a list" ,
所以。
不能识别为字符,而是列表
so it seems that .
is not recognized a character, but a list
您能告诉我如何避免此错误吗?
Could you tell me how to avoid this error?
推荐答案
您可以使用 .x
以访问元素
library(tidyverse)
c("a", "b") %>% map(~ tbl %>% select(c("id", .x)))
#[[1]]
# A tibble: 10 x 2
# id a
# <int> <dbl>
# 1 1 1.42
# 2 2 1.51
# 3 3 -0.385
#...
#[[2]]
# A tibble: 10 x 2
# id b
# <int> <dbl>
# 1 1 1.42
# 2 2 0.100
# 3 3 1.28
#....
您也可以使用。
,但在链式操作中使用。
指的是链左侧的对象,即 tbl
,因此返回一个错误。要使用。
一种方法是
You can also use .
but while using it in chain operation .
is referring to the object which is on the left-side of the chain i.e tbl
in this case , hence it returns an error. To use .
one way is
c("a", "b") %>% map(~select(tbl, c('id', .)))
这篇关于使用地图从小标题中生成小标题列表,然后选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文