小写某些单词 R [英] Lower Case Certain Words R

查看:39
本文介绍了小写某些单词 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将某些单词转换为小写.我正在处理一个电影片名列表,如果介词和冠词不是片名中的第一个单词,它们通常是小写的.如果我有向量:

I need to convert certain words to lower case. I am working with a list of movie titles, where prepositions and articles are normally lower case if they are not the first word in the title. If I have the vector:

movies = c('The Kings Of Summer', 'The Words', 'Out Of The Furnace', '我和伯爵以及垂死的女孩')

我需要的是这个:

movies_updated = c('夏日之王'、'话剧'、'走出熔炉'、'我和厄尔以及垂死的女孩')

是否有一种优雅的方法可以在不使用一长串 gsub() 的情况下做到这一点,例如:

Is there an elegant way to do this without using a long series of gsub(), as in:

movies_updated = gsub(' In ', ' in ', movies)
movies_updated = gsub(' In', ' in', movies_updated)
movies_updated = gsub(' Of ', ' of ', movies)
movies_updated = gsub(' Of', ' of', movies_updated)
movies_updated = gsub(' The ', ' the ', movies)
movies_updated = gsub(' the', ' the', movies_updated)

等等.

推荐答案

实际上,您似乎有兴趣将文本转换为 标题案例.这可以通过使用 stringi 轻松实现包,如下图:

In effect, it appears that you are interested in converting your text to title case. This can be easily achieved with use of the stringi package, as shown below:

>> stringi::stri_trans_totitle(c('The Kings of Summer', 'The Words', 'Out of the Furnace'))
[1] "The Kings Of Summer" "The Words"           "Out Of The Furnace"

替代方法将涉及使用 工具 包:

Alternative approach would involve making use of the toTitleCase function available in the the tools package:

>> tools::toTitleCase(c('The Kings of Summer', 'The Words', 'Out of the Furnace'))
[1] "The Kings of Summer" "The Words"           "Out of the Furnace" 

这篇关于小写某些单词 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆