在给定的数据集中,i在位置7处有cashothercash.other.

df <- data.frame(values = c("loan", "cash", "equity", 
"other", "commercial.news", "money.news", "cash.other"))

我试图返回grepl("cash.other", tolower(values)) ~ "Cash_Other",但它返回为Cash.

在原来的rabrame中,我也有cashothercash.other,这就是为什么我需要a返回cash.other作为Cash_Other.

df <- df %>%
  mutate(values = case_when(
    grepl("loan", tolower(values))  ~ "Lond",
    grepl("cash", tolower(values))  ~ "Cash",
    grepl("equity", tolower(values)) ~ "Equity",
    grepl("other", tolower(values)) ~ "Other",
    grepl("commercial.news", tolower(values)) ~ "Commercial_News",
    grepl("money.news", tolower(values)) ~ "Money_News",
    grepl("cash.other", tolower(values)) ~ "Cash_Other",
    TRUE ~ as.character(values)
  ))

df

cash.other的情况下,我如何返回Cash_Other

           values
1            Lond
2            Cash
3          Equity
4           Other
5 Commercial_News
6      Money_News
7            Cash

推荐答案

不确定Lond作为loan的替代品是否是一个错字,但如果你想转换为CamelCase,然后try 以下操作:

library(snakecase)
df <- data.frame(values = c("loan", "cash", "equity", 
                            "other", "commercial.news", "money.news", "cash.other"))
df$values2 <- snakecase::to_upper_camel_case(df$values, sep_out = "_")
#            values         values2
# 1            loan            Loan
# 2            cash            Cash
# 3          equity          Equity
# 4           other           Other
# 5 commercial.news Commercial_News
# 6      money.news      Money_News
# 7      cash.other      Cash_Other

R相关问答推荐

过滤器数据.基于两列的帧行和R中的外部向量

如何根据组大小应用条件过滤?

使用gcuminc,如何使用逗号格式化风险表?

在R中,如何在每个堆叠的条上放置误差条,特别是当使用facet_grid时?

使用ggsankey调整Sankey图中单个 node 上的标签

如何在geom_col中反转条

将文件保存到新文件夹时,切换r设置以不必创建目录

在R中按行按列范围查找最大值的名称

派生程序包| ;无法检索';return()';的正文

如何使这些react 表对象相互独立?

从R中发出的咕噜声中的BUG?

使用shiny 中的所选要素行下拉菜单

`-`是否也用于数据帧,有时使用引用调用?

是否可以将线性模型的p值添加到tbl_summary中

如何为混合模型输出绘制不同的线型?

在不重复主题的情况下重新排列组

有没有办法将勾选/审查标记添加到R中的累积关联图中?

从矩阵创建系数图

如何使用ggsurvfit包更改风险表中的标签名称?

使用相对风险回归计算RR