# 在R中,按特定字符分割字符向量;在新的矢量中保存第3件

`oss\$point <- unlist(strsplit(oss\$id, split='_', fixed=TRUE))[3]`

`strsplit(oss\$id, split= ‘_’, fixed=TRUE)`

```> head(oss\$point)
[[1]]
[1] "sil"  "2007" "1"

[[2]]
[1] "sil"  "2007" "2"

[[3]]
[1] "sil"  "2007" "3"

[[4]]
[1] "sil"  "2007" "4"

[[5]]
[1] "sil"  "2007" "5"

[[6]]
[1] "sil"  "2007" "6"```

strsplit创建一个列表,所以我会尝试以下方法：

```lapply(strsplit(oss\$id, split='_', fixed=TRUE), `[`, 3) ## Output a list
sapply(strsplit(oss\$id, split='_', fixed=TRUE), `[`, 3) ## Output a vector (even though a list is also a vector)```

[提取第三个元素的方法.如果你喜欢矢量,用sapply代替lapply.

```mystring <- c("A_B_C", "D_E_F")

lapply(strsplit(mystring, "_"), `[`, 3)
# [[1]]
# [1] "C"
#
# [[2]]
# [1] "F"
sapply(strsplit(mystring, "_"), `[`, 3)
# [1] "C" "F"```

```gsub(".*_.*_(.*)", "\\1", mystring)
# [1] "C" "F"```

```unlist(strsplit(mystring, "_"), use.names = FALSE)[c(FALSE, FALSE, TRUE)]
# [1] "C" "F"```

```gsub(".*_(.*)", "\\1", mystring)
# [1] "C" "F"```

```library(stringi)
stri_extract_last_regex(mystring, "[A-Z]+")
# [1] "C" "F"```