返回句子中最长的偶数长度单词

问题描述 投票:0回答:1

考虑以下数据

string <- data.frame(string=c('R is a popular programming language', 'python is good'))

                               string
1 R is a popular programming language
2                      python is good

expected string2 output 

                               string  string2
1 R is a popular programming language  language
2                      python is good  python

我尝试了以下方法,但没有达到预期效果

string <- data.frame(string=c('R is a popular programming language', 'python is good')) %>% 
  mutate(string2=strsplit(string, '\\s') %>% map(., \(x) which(str_length(x)%%2 == 0)),
         word=lapply(string, \(x) lapply(string2, \(y)  word(x,start=y, sep = '\\,')))
         )

您能帮我修复此代码吗,因为它给出了错误

r
1个回答
0
投票

首先创建一个函数来查找字符串中具有偶数个字符的最长单词:

get_max_even_word <- function(x) {
    len <- nchar(x)
    even_len <- ifelse(len %% 2 == 0, len, -1)
    x[which.max(even_len)]
}

然后将其应用到您的数据框:

string |>
    transform(
        string2 = strsplit(string, "\\s") |>
            sapply(get_max_even_word)
    )

#                                string  string2
# 1 R is a popular programming language language
# 2                      python is good   python
© www.soinside.com 2019 - 2024. All rights reserved.