r - 如果字符串有某个字符,则用某个值填充同一行中的一个空单元格
问题描述
假设我有以下数据框:
# S/N a b
# 1 L1-S2 <blank>
# 2 T1-T3 <blank>
# 3 T1-L2 <blank>
如何将上述数据框变成这样:
# S/N a b
# 1 L1-S2 LS
# 2 T1-T3 T
# 3 T1-L2 TL
我正在考虑写一个循环,其中
对于 a 列中的 x,
If first character in x == L AND 4th character in x == S,
fill the corresponding cell in b with LS
等等...
但是,我不确定如何实现它,或者是否有更优雅的方式来做到这一点。
解决方案
我们可以提取大写字母并删除重复的字母
library(stringr)
library(dplyr)
df1 %>%
mutate(b = str_replace(str_replace(a, "^([A-Z])\\d+-([A-Z])\\d+",
"\\1\\2"), "(.)\\1+", "\\1"))
-输出
# S_N a b
#1 1 L1-S2 LS
#2 2 T1-T3 T
#3 3 T1-L2 TL
或者另一种选择是str_extract_all
提取大写字母,遍历list
with map
,元素paste
unique
library(purrr)
df1 %>%
mutate(b = str_extract_all(a, "[A-Z]") %>%
map_chr(~ str_c(unique(.x), collapse="")))
或者对第一个 tidyverse 选项使用相应base R
的选项
df1$b <- sub("(.)\\1+", "\\1", gsub("[0-9-]+", "", df1$a))
或与strsplit
df1$b <- sapply(strsplit(df1$a, "[0-9-]+"),
function(x) paste(unique(x), collapse=""))
数据
df1 <- structure(list(S_N = 1:3, a = c("L1-S2", "T1-T3", "T1-L2"),
b = c(NA,
NA, NA)), class = "data.frame", row.names = c(NA, -3L))
推荐阅读
- django - Django:for循环遍历并行进程并存储值并在完成后返回
- google-bigquery - 如何从嵌套表 BigQuery 在嵌套表中插入数据
- c# - 如何从表中填充 DataGrid 列和从其他表 SQL 数据库中填充行 - C#
- php - 您好尝试构建注册页面但没有成功(网络错误),反应本机和 mysql
- python - 如何检查 SQL 服务器中的所有记录,存在于 MySQL 中?
- python - 为什么硒在 manjaro 上不能正常工作(PATH 问题)
- html - iframe 沙箱不尊重多个允许权限
- javascript - 有没有办法将数组发布到 web api 或 mvc 控制器并获取文件以下载结果?
- c# - 按一个元素排序并在同一个 LINQ 查询中返回多个属性
- c# - OpenTK (OpenGL) Lighting:为什么纹理只在对象的内部可见?