使用 purrr 包重命名名称以指定字符开头的列表元素
Rename list elements which has names starting with specified characters using purrr package
我有一个包含元素名称的列表,例如 x.height, x.weight, y.height, y.length, z.weight, z.price
我想提取名称以 "x."
开头的元素,并通过删除它们的前缀 "x."
来重命名这些元素。这可以分两步完成:
list.new <- list.old %>% keep(str_detect(names(.), "^x."))
names(list.new) <- str_replace(names(list.new), "x", "")
我的第一个问题:如何在管道中合并这两个步骤?
最后,我想处理所有不同前缀的列表 "y.", "z."
以获得包含重命名子列表的新列表,例如:
List of 3
$ x:List of 2
..$ height: num 100
..$ weight: num 200
$ y:List of 2
..$ height: num 300
..$ length: num 400
$ z:List of 2
..$ weight: num 500
..$ price: num 600
是否可以使用单个管道执行此操作?
您可以通过以下方式实现您想要的效果。请注意,这需要您拥有最新版本的 dplyr
软件包 (>= 1.0.0)。
library(dplyr)
library(stringr)
library(purrr)
list.old <- list(
x = list(x.height = 100, x.weight = 200),
y = list(y.height = 300, y.length = 400),
z = list(z.weight = 500, z.price = 600)
)
list.new <- list.old %>%
map(as_tibble) %>%
map(~ rename_with(.x, ~ str_remove(.x, "^[xyz]\."))) %>%
map(as.list)
str(list.new)
List of 3
$ x:List of 2
..$ height: num 100
..$ weight: num 200
$ y:List of 2
..$ height: num 300
..$ length: num 400
$ z:List of 2
..$ weight: num 500
..$ price : num 600
您可以简单地使用 setNames()
或 set_names()
:
list.old <- list(
x.height=1, x.weight=2, y.height=3, y.length=4, z.weight=5, z.price=6
)
list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
# $height
# [1] 1
#
# $weight
# [1] 2
并且要应用于许多前缀,请将前面的代码用作函数:
prefix_list <- c("x","y","z")
map(prefix_list,
function(prefix) list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
) %>%
set_names(prefix_list)
# $x
# $x$.height
# [1] 1
#
# $x$.weight
# [1] 2
#
#
# $y
# $y$.height
# [1] 3
#
# $y$.length
# [1] 4
#
#
# $z
# $z$.weight
# [1] 5
#
# $z$.price
# [1] 6
我有一个包含元素名称的列表,例如 x.height, x.weight, y.height, y.length, z.weight, z.price
我想提取名称以 "x."
开头的元素,并通过删除它们的前缀 "x."
来重命名这些元素。这可以分两步完成:
list.new <- list.old %>% keep(str_detect(names(.), "^x."))
names(list.new) <- str_replace(names(list.new), "x", "")
我的第一个问题:如何在管道中合并这两个步骤?
最后,我想处理所有不同前缀的列表 "y.", "z."
以获得包含重命名子列表的新列表,例如:
List of 3
$ x:List of 2
..$ height: num 100
..$ weight: num 200
$ y:List of 2
..$ height: num 300
..$ length: num 400
$ z:List of 2
..$ weight: num 500
..$ price: num 600
是否可以使用单个管道执行此操作?
您可以通过以下方式实现您想要的效果。请注意,这需要您拥有最新版本的 dplyr
软件包 (>= 1.0.0)。
library(dplyr)
library(stringr)
library(purrr)
list.old <- list(
x = list(x.height = 100, x.weight = 200),
y = list(y.height = 300, y.length = 400),
z = list(z.weight = 500, z.price = 600)
)
list.new <- list.old %>%
map(as_tibble) %>%
map(~ rename_with(.x, ~ str_remove(.x, "^[xyz]\."))) %>%
map(as.list)
str(list.new)
List of 3
$ x:List of 2
..$ height: num 100
..$ weight: num 200
$ y:List of 2
..$ height: num 300
..$ length: num 400
$ z:List of 2
..$ weight: num 500
..$ price : num 600
您可以简单地使用 setNames()
或 set_names()
:
list.old <- list(
x.height=1, x.weight=2, y.height=3, y.length=4, z.weight=5, z.price=6
)
list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
# $height
# [1] 1
#
# $weight
# [1] 2
并且要应用于许多前缀,请将前面的代码用作函数:
prefix_list <- c("x","y","z")
map(prefix_list,
function(prefix) list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
) %>%
set_names(prefix_list)
# $x
# $x$.height
# [1] 1
#
# $x$.weight
# [1] 2
#
#
# $y
# $y$.height
# [1] 3
#
# $y$.length
# [1] 4
#
#
# $z
# $z$.weight
# [1] 5
#
# $z$.price
# [1] 6