如何通过子字符串优雅地拆分列表元素中的字符串数组?

How to elegantly split array of strings in list elements by substring?

如何根据第一个字符优雅地将字符串数组拆分为子组?

示例数据:

c("1_autoa", "1_autob", "1_autoc","2_bier", "3_hundx", "3_hundy")

期望的输出:

[[1]]
[1] "1_autoa" "1_autob" "1_autoc"

[[2]]
[1] "2_bier"

[[3]]
[1] "3_hundx" "3_hundy"

list(
  c("1_autoa", "1_autob", "1_autoc"), c("2_bier"), c("3_hundx", "3_hundy"))

我试过的方法:(工作示例,但看起来不够长)

library(dplyr)
library(purrr)
library(magrittr)
data <- data.frame(
  id = 1:6, 
  name = c("1_autoa", "1_autob", "1_autoc", "2_bier", "3_hundx", "3_hundy")
)
data$start <- substr(x = data$name, start = 1, stop = 1)
spread(data, start, name) %>% 
  apply(MARGIN = 2, list) %>% 
  lapply(FUN = function(x) x[[1]][!is.na(x[[1]])])

简直

split(x, gsub('\D+', '', x))

其中,

x <- c("1_autoa", "1_autob", "1_autoc","2_bier", "3_hundx", "3_hundy")
v <- c("1_autoa", "1_autob", "1_autoc","2_bier", "3_hundx", "3_hundy")    
sapply(1:3, function(i) v[which(sapply(v, function(x) grepl(as.character(i), x)))])
[[1]]
[1] "1_autoa" "1_autob" "1_autoc"

[[2]]
[1] "2_bier"

[[3]]
[1] "3_hundx" "3_hundy"