r - 在列中按字母顺序对每个字符串进行排序
问题描述
如何按字母顺序对列中的每个单独的字符串进行排序?例如,下面是最佳输出的样子:
> x
Current Age Want
1 FrankGeorge 25 aeefggknorr
2 BobHenry 29 bbehnory
3 MichaelJohn 30 acehhijlmno
我只希望每个单独的单元格按字母顺序排序。我已经看到了R: Sort a string of items 按字母顺序和How to change the order of words with letters order 中的响应,不幸的是它们没有达到我的目标。
解决方案
我们可以用 拆分“当前”列,strsplit
循环遍历list
withsapply
和ed 字符paste
sort
sapply(strsplit(tolower(x$Current), ""), function(x) paste(sort(x), collapse=""))
#[1] "aeefggknorr" "bbehnory" "acehhijlmno"
数据
x <- structure(list(Current = c("FrankGeorge", "BobHenry", "MichaelJohn"
), Age = c(25L, 29L, 30L), Want = c("aeefggknorr", "bbehnory",
"acehhijlmno")), class = "data.frame", row.names = c("1", "2",
"3"))
推荐阅读
- c - 用户定义的一维数组的加法和平均函数
- python - 每次方法调用都会自动调用 __init__ 吗?
- ruby-on-rails - 检查字符串是否包含 Ruby 中的表情符号
- pandas-groupby - 按国家/地区对所有属性应用 z 分数
- r - R找到向量x中大于向量y中的值的值的计数
- javascript - 如何获取 HTML 文档特定部分(范围、片段)的 innerText?(即不是整个 HTML 元素)
- javascript - 如何将 p5.js 中加载的图像的配色方案从 RGB(我认为是默认值)转换为任何其他颜色方案,如 HSL?
- flutter - Flutter - 响应式图像映射?
- github - Github 站点在提交选项卡中显示已删除的提交
- ksh - 连接两个字符串以形成 shell 命令会导致奇怪的行为