Dear all, Given the following vector:> (z <- c('R project', 'hello world', 'something Else'))[1] "R project" "hello world" "something Else" I know how to obtain all capitals or all lower case letters:> tolower(z)[1] "r project" "hello world" "something else"> toupper(z)[1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" I saw the tocamel() function in 'rapport', but it doesn't do what I want to achieve as it actually proceeds to camelCase/CamelCase the strings:> tocamel(z)[1] "RProject" "helloWorld" "somethingElse" But how should I proceed to obtain Camel Case? Here's what I'd like to get: c('R Project', 'Hello World', 'Something Else') Regards, Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
Hi, There is an example of how to do do what you are looking for in ?toupper Regards, Pascal On 04/15/2013 03:50 PM, Liviu Andronic wrote:> Dear all, > Given the following vector: >> (z <- c('R project', 'hello world', 'something Else')) > [1] "R project" "hello world" "something Else" > > I know how to obtain all capitals or all lower case letters: >> tolower(z) > [1] "r project" "hello world" "something else" >> toupper(z) > [1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" > > I saw the tocamel() function in 'rapport', but it doesn't do what I > want to achieve as it actually proceeds to camelCase/CamelCase the > strings: >> tocamel(z) > [1] "RProject" "helloWorld" "somethingElse" > > > But how should I proceed to obtain Camel Case? Here's what I'd like to get: > c('R Project', 'Hello World', 'Something Else') > > > Regards, > Liviu > >
See for instance capitalize() in the R.utils package. Henrik On Apr 14, 2013 11:51 PM, "Liviu Andronic" <landronimirc@gmail.com> wrote:> Dear all, > Given the following vector: > > (z <- c('R project', 'hello world', 'something Else')) > [1] "R project" "hello world" "something Else" > > I know how to obtain all capitals or all lower case letters: > > tolower(z) > [1] "r project" "hello world" "something else" > > toupper(z) > [1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" > > I saw the tocamel() function in 'rapport', but it doesn't do what I > want to achieve as it actually proceeds to camelCase/CamelCase the > strings: > > tocamel(z) > [1] "RProject" "helloWorld" "somethingElse" > > > But how should I proceed to obtain Camel Case? Here's what I'd like to get: > c('R Project', 'Hello World', 'Something Else') > > > Regards, > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Apr 15, 2013, at 08:50 , Liviu Andronic wrote:> Dear all, > Given the following vector: >> (z <- c('R project', 'hello world', 'something Else')) > [1] "R project" "hello world" "something Else" > > I know how to obtain all capitals or all lower case letters: >> tolower(z) > [1] "r project" "hello world" "something else" >> toupper(z) > [1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" > > I saw the tocamel() function in 'rapport', but it doesn't do what I > want to achieve as it actually proceeds to camelCase/CamelCase the > strings: >> tocamel(z) > [1] "RProject" "helloWorld" "somethingElse" > > > But how should I proceed to obtain Camel Case? Here's what I'd like to get: > c('R Project', 'Hello World', 'Something Else')That'll be capitalization, not camel case, camel-casing is when wordsHaveHumps inTheMiddle. As for actually solving your problem, I'd try googling for "regular expression to capitalize word" and convert to use with gsub(). (I did do the googling, but regexps being what they are - unreadable gibberish at first sight - I thought I'd leave the hard work for others...). Or, as Pascal points out: example(toupper). -pd> > > Regards, > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Dear Liviu, I have just updated "tocamel" to have a new argument, so the development version of the package would produce:> tocamel(z, upper = TRUE, sep = ' ')[1] "R Project" "Hello World" "Something Else" Best, Gergely PS #1: to install the "dev" branch you might give a try to the devtools package:> library(devtools) > install_github('rapport', 'rapporter', 'development')PS #2: Alex (cc) pls verify. On 15 April 2013 08:50, Liviu Andronic <landronimirc@gmail.com> wrote:> Dear all, > Given the following vector: > > (z <- c('R project', 'hello world', 'something Else')) > [1] "R project" "hello world" "something Else" > > I know how to obtain all capitals or all lower case letters: > > tolower(z) > [1] "r project" "hello world" "something else" > > toupper(z) > [1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" > > I saw the tocamel() function in 'rapport', but it doesn't do what I > want to achieve as it actually proceeds to camelCase/CamelCase the > strings: > > tocamel(z) > [1] "RProject" "helloWorld" "somethingElse" > > > But how should I proceed to obtain Camel Case? Here's what I'd like to get: > c('R Project', 'Hello World', 'Something Else') > > > Regards, > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
HI, You could use: gsub("(^|\\s+)([a-z])","\\1\\U\\2",z,perl=TRUE) #[1] "R Project"????? "Hello World"??? "Something Else" #or ?gsub("\\b([a-z])","\\U\\1",z,perl=TRUE) #[1] "R Project"????? "Hello World"??? "Something Else" A.K. ----- Original Message ----- From: Liviu Andronic <landronimirc at gmail.com> To: r-help <r-help at stat.math.ethz.ch> Cc: Sent: Monday, April 15, 2013 2:50 AM Subject: [R] how to transform string to "Camel Case"? Dear all, Given the following vector:> (z <- c('R project', 'hello world', 'something Else'))[1] "R project"? ? ? "hello world"? ? "something Else" I know how to obtain all capitals or all lower case letters:> tolower(z)[1] "r project"? ? ? "hello world"? ? "something else"> toupper(z)[1] "R PROJECT"? ? ? "HELLO WORLD"? ? "SOMETHING ELSE" I saw the tocamel() function in 'rapport', but it doesn't do what I want to achieve as it actually proceeds to camelCase/CamelCase the strings:> tocamel(z)[1] "RProject"? ? ? "helloWorld"? ? "somethingElse" But how should I proceed to obtain Camel Case? Here's what I'd like to get: c('R Project', 'Hello World', 'Something Else') Regards, Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Mon, Apr 15, 2013 at 2:50 AM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all, > Given the following vector: >> (z <- c('R project', 'hello world', 'something Else')) > [1] "R project" "hello world" "something Else" > > I know how to obtain all capitals or all lower case letters: >> tolower(z) > [1] "r project" "hello world" "something else" >> toupper(z) > [1] "R PROJECT" "HELLO WORLD" "SOMETHING ELSE" > > I saw the tocamel() function in 'rapport', but it doesn't do what I > want to achieve as it actually proceeds to camelCase/CamelCase the > strings: >> tocamel(z) > [1] "RProject" "helloWorld" "somethingElse" > > > But how should I proceed to obtain Camel Case? Here's what I'd like to get: > c('R Project', 'Hello World', 'Something Else') >Here is a one liner using gsubfn from the gsubfn package (http://gsubfn.googlecode.com). gsubfn is like gsubfn except the second argument can be a function (or a list or a proto object). The regular expression here matches a space followed by any character. For each such match gsubfn will call the function denoted by the second argument using the two parenthesized portions of the regular expression as the two arguments. It supports an optional formula notation to express the function for compactness so ... ~ toupper(..2) denotes function(...) toupper(..2) which is equivalent to function(x, y) toupper(y) . It replaces the input with the output of that function. Finally we check if any of the components of the input are NA and replace those with NA in the output:> replace(gsubfn("( )(.)", ... ~ toupper(..2), z), is.na(z), NA)[1] "RProject" "helloWorld" "somethingElse" NA