如何在不影响 R 中的数字的情况下反转字符串?
How to reverse a string without effecting the numbers in R?
我想反转字符串但不影响数字。
例如:
输入:"abcdef 123"
输出:"fedcba 123"
目前我正在使用我在这里找到的这个函数,但它会影响所有字符:
name<-sapply(strsplit(name, split = ""),
function(str) {paste(rev(str), collapse = "")})
这是一种方法:
案例一:
a <- "abcdef 123"
# split the string
split_a <- unlist(strsplit(a, " "))
# reverse it
paste(paste(rev(strsplit(split_a[1],'')[[1]]), collapse = ''), split_a[2])
"fedcba 123"
案例二:
a <- "abc def 123"
# split the string
split_a <- unlist(strsplit(a, " "))
# removing the last word
to_split <- split_a[-length(split_a)]
reversed <- paste(sapply(lapply(strsplit(to_split, NULL), rev), paste, collapse=''), collapse=' ')
final <- paste(reversed, split_a[length(split_a)], collapse=" ")
[1] "cba fed 123"
稳健的解决方案:
# convert vector of strings into list of vectors of words
words = strsplit(name, ' ', fixed = TRUE)
str_rev = sapply(words, function(x) {
# we know some warnings may be created, ignore them
suppressWarnings({
is_num = !is.na(as.numeric(x))
})
# reverse non-numeric elements
str_words = strsplit(x[!is_num], "", fixed = TRUE)
x[!is_num] = sapply(str_words, function(y) {
paste(rev(y), collapse = "")
})
paste(x, collapse = ' ')
})
这允许在您不知道数字 "word" 将出现在哪里以及 name
作为向量的更一般情况下应用该规则:
name = c("abcdef 123", 'abc def 123', 'abc 123 def')
str_rev
# [1] "fedcba 123" "cba fed 123" "cba 123 fed"
这是一个使用 stringi
中的 stri_reverse
的选项
library(stringi)
library(gsubfn)
gsubfn("([^0-9 ]+)", ~ stri_reverse(x), name)
#[1] "fedcba 123" "cba fed 123" "cba 123 fed"
或@G。 Grothendieck提到,匿名函数调用可以去掉
gsubfn("([^0-9 ]+)", stri_reverse, name)
数据
name <- c("abcdef 123", 'abc def 123', 'abc 123 def')
我想反转字符串但不影响数字。 例如:
输入:"abcdef 123" 输出:"fedcba 123"
目前我正在使用我在这里找到的这个函数,但它会影响所有字符:
name<-sapply(strsplit(name, split = ""),
function(str) {paste(rev(str), collapse = "")})
这是一种方法:
案例一:
a <- "abcdef 123"
# split the string
split_a <- unlist(strsplit(a, " "))
# reverse it
paste(paste(rev(strsplit(split_a[1],'')[[1]]), collapse = ''), split_a[2])
"fedcba 123"
案例二:
a <- "abc def 123"
# split the string
split_a <- unlist(strsplit(a, " "))
# removing the last word
to_split <- split_a[-length(split_a)]
reversed <- paste(sapply(lapply(strsplit(to_split, NULL), rev), paste, collapse=''), collapse=' ')
final <- paste(reversed, split_a[length(split_a)], collapse=" ")
[1] "cba fed 123"
稳健的解决方案:
# convert vector of strings into list of vectors of words
words = strsplit(name, ' ', fixed = TRUE)
str_rev = sapply(words, function(x) {
# we know some warnings may be created, ignore them
suppressWarnings({
is_num = !is.na(as.numeric(x))
})
# reverse non-numeric elements
str_words = strsplit(x[!is_num], "", fixed = TRUE)
x[!is_num] = sapply(str_words, function(y) {
paste(rev(y), collapse = "")
})
paste(x, collapse = ' ')
})
这允许在您不知道数字 "word" 将出现在哪里以及 name
作为向量的更一般情况下应用该规则:
name = c("abcdef 123", 'abc def 123', 'abc 123 def')
str_rev
# [1] "fedcba 123" "cba fed 123" "cba 123 fed"
这是一个使用 stringi
stri_reverse
的选项
library(stringi)
library(gsubfn)
gsubfn("([^0-9 ]+)", ~ stri_reverse(x), name)
#[1] "fedcba 123" "cba fed 123" "cba 123 fed"
或@G。 Grothendieck提到,匿名函数调用可以去掉
gsubfn("([^0-9 ]+)", stri_reverse, name)
数据
name <- c("abcdef 123", 'abc def 123', 'abc 123 def')