英文:
use named vector to create column in pipe chain
问题
在管道链中,您想要使用一个命名向量来创建一个新列,该新列与向量的名称匹配字符串列的字符串:
library(tidyverse)
df <- data.frame(my_label = c("car", "house", "Bike", "ca"),
xx = c(1, 2, 3, 5))
named_vars <- c(
"car" = "Nice car",
"ca" = "Cat",
"house" = "Large house"
)
以下是可以实现您所需的输出的代码:
df %>%
mutate(new = coalesce(named_vars[my_label], my_label))
这将为您提供以下结果:
my_label xx new
1 car 1 Nice car
2 house 2 Large house
3 Bike 3 Bike
4 ca 5 Cat
希望这有所帮助!
英文:
In a pipe chain, I want to use a named vector to create a new column which matches the names of the vector with the string of a column:
library(tidyverse)
df <- data.frame(my_label = c("car", "house", "Bike", "ca"),
xx = c(1, 2, 3, 5))
# my_label xx
# 1 car 1
# 2 house 2
# 3 Bike 3
# 4 ca 5
named_vars <- c(
"car" = "Nice car",
"ca" = "Cat",
"house" = "Large house")
# car ca house
# "Nice car" "Cat" "Large house"
The following code works if the named vector contains all the strings within the column, which it doesn't in this case so it returns an NA
(if it is missing I want to keep the original (Bike
in this example):
df %>%
mutate(new = named_vars[my_label])
# my_label xx new
# 1 car 1 Nice car
# 2 house 2 Large house
# 3 Bike 3 <NA>
# 4 ca 5 Cat
As a workaround, this produces the output I want:
df %>%
mutate(new = ifelse(my_label %in% names(named_vars), named_vars[my_label], my_label))
# my_label xx new
# 1 car 1 Nice car
# 2 house 2 Large house
# 3 Bike 3 Bike
# 4 ca 5 Cat
I am wondering is there a shorter way to write this?
I tried stringr::str_replace_all
but it combines ca
and car
to give incorrect output (Nice Catr
instead of Nice car
):
library(stringr)
df %>%
mutate(new = str_replace_all(my_label, named_vars))
# my_label xx new
# 1 car 1 Nice Catr
# 2 house 2 Large house
# 3 Bike 3 Bike
# 4 ca 5 Cat
Any suggestions? thanks
答案1
得分: 1
使用 coalesce
:
df %>%
mutate(new = coalesce(named_vars[my_label], my_label))
# my_label xx new
# 1 car 1 美丽的车
# 2 house 2 大房子
# 3 Bike 3 自行车
# 4 ca 5 猫
这两个语句在这里具有相同的功能:
coalesce(named_vars[my_label], my_label)
if_else(is.na(named_vars[my_label]), my_label, named_vars[my_label])
英文:
Use coalesce
:
df %>%
mutate(new = coalesce(named_vars[my_label], my_label))
# my_label xx new
# 1 car 1 Nice car
# 2 house 2 Large house
# 3 Bike 3 Bike
# 4 ca 5 Cat
These two statements are functionality equivalent here:
coalesce(named_vars[my_label], my_label)
if_else(is.na(named_vars[my_label]), my_label, named_vars[my_label])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论