r - R regexp 用于对 char 向量进行奇数排序
问题描述
我有数百个文件需要以复杂的方式对其列进行排序。想象一个字符向量x
,它是names(foo)
where foo
is a的结果data.frame
:
x <- c("x1","i2","Component.1","Component.10","Component.143","Component.13",
"r4","A","C16:1n-7")
我想根据以下规则对其进行排序:首先,以“组件”开头的任何内容都按字母顺序排列。其次,以“C”和数字开头的任何剩余内容按字母顺序排列。第三个按字母顺序剩余的东西。
因为x
那将是:
x[c(3,4,6,5,9,8,2,7,1)]
这是regexp
一种任务吗?一个人用match
吗?每个文件将具有不同数量的列(因此x
将具有不同的长度)。任何提示表示赞赏。
解决方案
您可以使用以下功能order
来实现base-r
:
x <- c("x1","i2","Component.1","Component.10","Component.143","Component.13",
"r4","A","C16:1n-7")
order(
!startsWith(x, "Component"), # 0 - starts with component, 1 - o.w.
!grepl("^C\\d", x), # 0 - starts with C<NUMBER>, 1 - o.w.
x # alphabetical
)
# output: 3 4 6 5 9 8 2 7 1
推荐阅读
- python - 使用 plot.ly Dash 等待结果时显示加载符号
- java - 基于前景/背景均值计算二值最优阈值 ImageJ
- c# - 如何检查文件是否被第三方应用程序修改?我正在寻找微小的代码示例
- mysql - 如何在sql中设置列的每个不同值的唯一名称
- python - 我在 Windows 10、python 3.7 的 anaconda 中安装 igraph 时出现代理错误
- regex - Notepad++ html标签/字符串(a href)替换
- java - Java - NumberValue 到没有货币符号的字符串
- php - 用于从文件加载的 php 图像的 css 中的图像大小
- python - 根据条目对 JSON 数组进行排序和重新排列
- html - 如何使链接只能在文本上点击