简体   繁体   English

如何重新排序 R 中的字符串以遵循一致的模式

[英]How to reorder a string in R to follow a consistent pattern

I have vector of strings of the type: 2004/083.BHP , 2007.MAIS.0048 and 2006/0066 .我有以下类型的字符串向量: 2004/083.BHP2007.MAIS.00482006/0066 There are lots of these strings of varying characters.有很多这些不同字符的字符串。

I would like to have consistentcy in the representation of these strings, so that they look something like 083/2004.BHP , 0048/2007.MAIS and 0066/2006 .我希望在这些字符串的表示中保持一致,以便它们看起来像083/2004.BHP0048/2007.MAIS0066/2006

How do I get all strings in this vector to appear in this way without manually coding it?如何在不手动编码的情况下让这个向量中的所有字符串以这种方式出现? I understand this is a difficult task, and any help is appreciated.我知道这是一项艰巨的任务,任何帮助表示赞赏。

Thank you in advance.先感谢您。

Here are a few suggestions for ordering or sorting your strings in a consistent pattern (such as by alphabetic order or by number of characters).以下是一些关于以一致模式(例如按字母顺序或按字符数)对字符串进行排序或排序的建议。 The last case, sorts by starting with a 4-digit (ie date) and then by name.最后一种情况,以 4 位数字(即日期)开头,然后按名称排序。

  strings <- c("2004/083.BHP","2007.MAIS.0048","2006/0066","432.ABC","2008/42002","2094/31.AC")
strings <- sort(strings)
strings
strings <- strings[order(nchar(strings))]
strings
strings <- strings[order(strings,decreasing =T) ]
strings
strings <- strings[order(grepl("^\\d{4}",strings),strings,decreasing =F) ]
strings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM