r - 将 stringr::str_glue 与管道一起使用
问题描述
我想str_glue
与管道一起使用。我的代码:
library(tidyverse)
x <- c('john', 'bell', 'mary', 'cott')
x %>%
str_to_title(.) %>%
str_glue('Hi, {.}. How are you?')
但是,出现以下错误消息:
错误:所有未命名参数的长度必须为 1
期望输出:
Hi, John. How are you?
Hi, Bell. How are you?
Hi, Mary. How are you?
Hi, Cott. How are you?
解决方案
查看帮助help(str_glue)
,我想出了以下工作解决方案。您可以自己查看帮助。
x <- c('john', 'bell', 'mary', 'cott')
x <- data.frame(x)
rownames(x) <- x[,1]
x %>%
str_glue_data("Hi, {rownames(.)}. How are you?")
#Hi, john. How are you?
#Hi, bell. How are you?
#Hi, mary. How are you?
#Hi, cott. How are you?
您也可以在str_glue
不使用管道的情况下使用如下(我认为这不是您想要的,所以上面的数据框解决方法)
x <- c('john', 'bell', 'mary', 'cott')
str_glue('Hi, {x}. How are you?')
#Hi, john. How are you?
#Hi, bell. How are you?
#Hi, mary. How are you?
#Hi, cott. How are you?
希望有帮助。
推荐阅读
- postman - 是否有解决方法来确保共享工作区中的集合未存档在 Postman Free Team 帐户中?
- salesforce - 删除联系人时显示最终异常
- php - 获取树枝数组中数组的值
- java - 在 Spring Thymeleaf Java 项目中使用 AJAX 调用时发生错误 com.fasterxml.jackson.databind
- ampl - 在 AMPL 中,如何引用部分结果,并在多个地方使用它们
- docker - 拉 nanoserver:1903 时,“清单列表条目中没有匹配的清单”
- outlook-web-addins - 在应用商店上传清单文件时出现“remoteurl”错误
- javascript - 从节点js中的缓冲区检测文件类型?
- javascript - 解释在 Javascript ES6 中调用对象方法的函数中“this”的使用
- sql - SQL 仅替换路径的一部分