我有一个字符串列表,我必须将某个子字符串从caps更改为lowcaps.如何在R中有效地实现这一点?
这是一个子列表:
>head(ID)
"1007_PM_S_AT"
"1053_PM_AT"
"117_PM_AT"
"121_PM_AT"
"1255_PM_G_AT"
"1294_PM_AT"
Run Code Online (Sandbox Code Playgroud)
我需要在PM之后将所有内容更改为小写.
一种选择是包裹tolower()一个sub()电话
R> test <- c("1007_PM_S_AT", "1053_PM_AT", "117_PM_AT", "121_PM_AT", "1255_PM_G_AT", "1294_PM_AT")
R> sub("pm", "PM", tolower(test))
[1] "1007_PM_s_at" "1053_PM_at" "117_PM_at" "121_PM_at" "1255_PM_g_at" "1294_PM_at"
Run Code Online (Sandbox Code Playgroud)
可能有用的另一种替代方案(可能不太好)是使用替换功能regmatches<-.
matches <- gregexpr('(?<=PM)(.+)', test, perl=TRUE) # match the string after PM
regmatches(test, matches) <- tolower(regmatches(test, matches)) # replace with lower case
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |