1

我想更改不同植物分布的不同地图文件的名称。

现在他们有这个物种的全名,比如:

Amaranthus australis.kml
Capsicum annuum.kml
Cucurbita moschata.kml
Ipomoea alba.kml
Persea donnell-smithii.kml

我想用每个单词的前 3 个字符来命名它们,如下所示:

ama_aus.kml
cap_ann.kml
cuc_mos.kml
ipo_alb.kml
Per_don.kml

有谁知道如何在 R 中做到这一点?

4

2 回答 2

6

这可能很有用

Names <- c("Amaranthus australis.kml",
           "Capsicum annuum.kml",
           "Cucurbita moschata.kml",
           "Ipomoea alba.kml",
           "Persea donnell-smithii.kml")

sapply (lapply(strsplit(tolower(Names), " "), substr, 1, 3), 
        function(x) paste(paste(x[1], x[2], sep="_"),"kml", sep="."))
[1] "ama_aus.kml" "cap_ann.kml" "cuc_mos.kml" "ipo_alb.kml" "per_don.kml"
于 2013-05-28T13:44:27.267 回答
5

paste你可以这样做substr

lines <- 'Amaranthus australis.kml
Capsicum annuum.kml
Cucurbita moschata.kml
Ipomoea alba.kml
Persea donnell-smithii.kml'

x <- read.table(text=lines, as.is=TRUE)

paste(tolower(substr(x[, 1], 1, 3)), 
      paste(substr(x[, 2], 1, 3), tools::file_ext(x[, 2]), sep="."),
      sep="_")
# [1] "ama_aus.kml" "cap_ann.kml" "cuc_mos.kml" "ipo_alb.kml" "per_don.kml"

编辑: 这是一种使用正则表达式的方法,但我更喜欢另一种方法,因为它似乎会减少边缘情况。

text <- strsplit(lines, "\n")[[1]]
tolower(sub("^(\\w{3})\\w* (\\w{3})[A-Za-z-]+\\.(kml)$", "\\1_\\2.\\3", text))
于 2013-05-28T13:52:58.230 回答