Lower Case Certain Words R
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"
Capitalize the first letter of both words in a two word string
The base R function to perform capitalization is toupper(x)
. From the help file for ?toupper
there is this function that does what you need:
simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep="", collapse=" ")
}
name <- c("zip code", "state", "final count")
sapply(name, simpleCap)
zip code state final count
"Zip Code" "State" "Final Count"
Edit This works for any string, regardless of word count:
simpleCap("I like pizza a lot")
[1] "I Like Pizza A Lot"
Convert part of string to upper (or lower) case
We can use regex lookarounds. We match and capture a word starting with lower case letter followed by regex lookahead number ((?=[0-9])
) as a group (using parentheses) and in the replacement we use \\U
followed by the capture group to convert it to upper case.
sub('\\b([a-z])(?=[0-9])', '\\U\\1', test, perl=TRUE)
#[1] "Aa, Heeswijk T1" "Aa, Heeswijk T1" "Aa, Middelrode T2"
#[4] "Meander Assendelft P1" "Aa, Heeswijk T1a" "Aa, Heeswijk T3b"
Or without using the lookarounds, we can do this with two capture groups.
sub('\\b([a-z])([0-9])', '\\U\\1\\2', test, perl=TRUE)
Update
Testing with the updated 'test' from the OP's post
sub('\\b([a-z])(?=[0-9])', '\\U\\1', test, perl=TRUE)
#[1] "Aa, Heeswijk T1" "Aa, Heeswijk T1" "Aa, Middelrode T2"
#[4] "Aa, Middelrode P1" "Aa, Heeswijk T1a" "Aa, Heeswijk T3b"
#[7] "Aa, test1 T1" "Aa, test2 T1"
Change text to lowercase in R keeping acronyms in uppercase in text mining
We can do:
test is the input:
paste(lapply(strsplit(test," "),function(x) ifelse(x %in% toupper(tm::stopwords()),
tolower(x),x))[[1]],collapse=" ")
[1] "NASA is a US COMPANY"
In R, switch uppercase to lowercase and vice-versa in a string
I'm sort of curious if there is a better way than:
chartr(x = this,
old = paste0(c(letters,LETTERS),collapse = ""),
new = paste0(c(LETTERS,letters),collapse = ""))
Helpful observation by @Joris in the comments that ?chartr
notes that you can use character ranges, avoiding the paste
:
chartr("a-zA-Z", "A-Za-z",this)
How to remove words not in caps in R?
Just use grep
and a regular expression:
words <- 'Albert Einstein went to the store and saw his friend Nikola Tesla'
# split to vector of individual words
vec <- unlist(strsplit(words, ' '))
# just the capitalized ones
caps <- grep('^[A-Z]', vec, value = T)
# assemble back to a single string, if you want
paste(caps, collapse=' ')
Converting the characters of strings in a single column into lowercase and getting the updated dataframe with all columns
The tolower
function should do the trick like so:
df <- df %>%
mutate(colB = tolower(colB)
How to capitalize all but some letters in R
Try
v2 <- gsub("[sty]", "", paste(letters, collapse=""))
chartr(v2, toupper(v2), v1)
#[1] "JAStADMMNIsyNDK" "LAUKsNDTUsAINS"
data
v1 <- c("JaStADmmnIsynDK", "laUksnDTusainS")
Replace all characters, except the first, in every word of a string with lowercase
There is a pretty way to do this. We can make a single call to gsub
in Perl mode, taking advantage of the ability to lowercase a capture group.
text <- "This String IS a tESt. TRYING TO fINd a waY to do ThiS."
gsub("(?<=\\b.)(.*?)\\b", "\\L\\1", text, perl=TRUE)
[1] "This String Is a test. Trying To find a way to do This."
Demo
Related Topics
R Table Function - How to Remove 0 Counts
Sum Columns by Group (Row Names) in a Matrix
Get First Entries in Rows of List
Convert Data with One Column and Multiple Rows into Multi Column Multi Row Data
R: Using "Microbenchmark" and Ggplot2 to Plot Runtimes
R: How to Judge Date in the Same Week
R Convert String Date (E.G. "October 1, 2014") to Date Format
Error in Install.Packages:Type =="Both" Cannot Be Used with 'Repos =Null'
Dummy Variables to Single Categorical Variable (Factor) in R
Multiplying Vector Combinations
Removing Everything After First 'Backslash' in a String
R Packages Fail to Compile with Gcc
Replace Na with Grouped Means in R
Unexpected Date When Converting Posixct Date-Time to Date - Timezone Issue