如何根据 R 中特定字符模式后出现的定界符拆分以下字符串向量?
How do you split the following vector of strings based on a delimiter that occurs after a certain character pattern in R?
这里是向量的一个例子:
strings<- (c("SPG_L_SPG_R", "SAS_SPG_R_SFG_L", "s_cere_R_SPG_L" ))
我需要拆分字符串 "SPG_L", "SPG_R","SAS_SPG_R", "SFG_L", "s_cere_R", "SPG_L"
我想在“_L”或“_R”之后出现的“_”处拆分字符串
我知道有一种使用正则表达式拆分字符串的方法,然后我想使用应用函数将字符串拆分函数应用于整个向量。我已经在论坛上搜索了示例来帮助我做到这一点,但我仍在苦苦挣扎。感谢您的帮助!
使用 positive look-behind assertion,我们可以在 _
处拆分,前面有 R 或 L
stringr::str_split(strings, '(?<=[RL])_', simplify = TRUE)
[,1] [,2]
[1,] "SPG_L" "SPG_R"
[2,] "SAS_SPG_R" "SFG_L"
[3,] "s_cere_R" "SPG_L"
这里是向量的一个例子:
strings<- (c("SPG_L_SPG_R", "SAS_SPG_R_SFG_L", "s_cere_R_SPG_L" ))
我需要拆分字符串 "SPG_L", "SPG_R","SAS_SPG_R", "SFG_L", "s_cere_R", "SPG_L"
我想在“_L”或“_R”之后出现的“_”处拆分字符串
我知道有一种使用正则表达式拆分字符串的方法,然后我想使用应用函数将字符串拆分函数应用于整个向量。我已经在论坛上搜索了示例来帮助我做到这一点,但我仍在苦苦挣扎。感谢您的帮助!
使用 positive look-behind assertion,我们可以在 _
处拆分,前面有 R 或 L
stringr::str_split(strings, '(?<=[RL])_', simplify = TRUE)
[,1] [,2]
[1,] "SPG_L" "SPG_R"
[2,] "SAS_SPG_R" "SFG_L"
[3,] "s_cere_R" "SPG_L"