将向量中的所有其他元音设为大写

U10*_*ard 6 string r uppercase

给定一个小写字符串。前任:

s <- 'abcdefghijklmnopqrstuvwxyz'
Run Code Online (Sandbox Code Playgroud)

目标是使字符串中的所有其他元音都大写。

此处所需的输出:

abcdEfghijklmnOpqrstuvwxyz
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,因为所有元音都是按顺序使用的,e并且o都是大写的。

在所有情况下,字符串中都只有小写字符。

对于aieou,所需的输出是:

aIeOu
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 R 中做到这一点?

我试过:

s[unlist(strsplit(s, '')) %in% c('a', 'e', 'i', 'o', 'u')] <- toupper(s[unlist(strsplit(s, '')) %in% c('a', 'e', 'i', 'o', 'u')])
Run Code Online (Sandbox Code Playgroud)

但无济于事。

即使这有效,也不会是所有其他元音

R 版本 4.1.1。

Joe*_*Joe 5

这不是一句单行话,而是:

s <- 'abcdefghijklmnopqrstuvwxyz'

as_list <- unlist(strsplit(s, ''))
vowels <- as_list %in% c('a', 'e', 'i', 'o', 'u')
every_other_index <- which(vowels)[c(FALSE, TRUE)]

as_list[every_other_index] <- toupper(as_list[every_other_index])

print(paste(as_list, collapse=''))
Run Code Online (Sandbox Code Playgroud)

给出:

[1] "abcdEfghijklmnOpqrstuvwxyz"
Run Code Online (Sandbox Code Playgroud)

(使用which来自这个问题;使用c(FALSE, TRUE)] 来自这里。)