Skip to content

Is there a simple method of changing text into 'Proper Ca se'

7 messages · Mulholland, Tom, Uwe Ligges, Spencer Graves +4 more

#
Yes and no. Given your response it appears that "Proper Case" is not a term
that everyone uses. In Excel there is a function "Proper" which in essence
changes "this line into something like this" into "This Line Into Something
Like This."

My look at casefold seesm to be that is is a wrapper of two functions to
change text into either Lower or Upper case.So my question is about how do
you just capitalise the first letter in each word.

Thanks for your response.

Tom

-----Original Message-----
From: Spencer Graves [mailto:spencer.graves at PDF.COM] 
Sent: Wednesday, 14 May 2003 12:51 PM
To: Mulholland, Tom
Cc: ' (r-help at stat.math.ethz.ch)'
Subject: Re: [R] Is there a simple method of changing text into 'Proper
Case'


It's not obvious to me what you are asking, but I'm guessing that 
"casefold" might help.

hth.  spencer graves
Mulholland, Tom wrote:
#
Mulholland, Tom wrote:
Perl experts might do it differently, but the "R way" seems to be

  substring(x, 1, 1) <- toupper(substring(x, 1, 1))
  substring(x, 2) <- tolower(substring(x, 2))

Uwe Ligges
#
Excellent.  Users concered about transportability to S-Plus would have 
to translate "toupper" and "tolower" into "casefold", however.

Spencer Graves
Uwe Ligges wrote:
#
Uwe Ligges wrote:
I hadn't come across this use of substring on the left hand side of <-
before. Now, of course, I see it explained on the help page for substr.

What other special functions can appear on the left hand side of <-? Is
there anywhere I can look to find a list? I've come across
  vector[vector of logicals] <- 
  vector[vector of integers] <- 
  data.frame[] <-

One thing I would find very handy is a shortcut for
  res <- myfunc()
  a <- res$val1
  b <- res$val2
Something along the lines of
  list(a=val1,b=val2) <- myfunc()
but I don't know what the right syntax would be or how I'd go about
programming it. Any suggestions?

Damon Wischik.
#
These are called `replacement functions', or sometimes `assignment
functions'.  Leaving aside S4 methods (set by setReplaceMethod), they all
end in "<-", as the first quoted example is actually evaluated as 
something very close to

x <- "substring<-"(x, 1, 1, toupper(substring(x, 1, 1)))

In the base package there are
[1] "$<-"                "<-"                 "<<-"
 [4] "[<-"                "[[<-"               "attr<-"
 [7] "attributes<-"       "body<-"             "class<-"
[10] "codes<-"            "colnames<-"         "comment<-"
[13] "contrasts<-"        "diag<-"             "dim<-"
[16] "dimnames<-"         "environment<-"      "formals<-"
[19] "is.na<-"            "length<-"           "levels<-"
[22] "mode<-"             "mostattributes<-"   "names<-"
[25] "oldClass<-"         "parent.env<-"       "row.names<-"
[28] "rownames<-"         "split.data.frame<-" "split<-"
[31] "storage.mode<-"     "substr<-"           "substring<-"
[34] "tsp<-"

(and some methods; "split.data.frame<-" is a mistake and codes<- is 
deprecated in R-devel).

But users can write them as well, and there are some in the methods 
package.
On Wed, 14 May 2003, Damon Wischik wrote:

            

  
    
1 day later
#
Damon Wischik <djw1005 at cam.ac.uk> wrote:
I agree this would be handy!  And appealing to Perl folks who are used to:
  Perl> ($arg1, $arg2) = @ARGV;
I couldn't figure out how to do it with replacement functions (see Prof Brian
Ripley's <ripley at stats.ox.ac.uk> reply), but here's another approach:

multi.assign <- function(x, ...) {
  mycall <- match.call()[-2]
  mycall[1] <- call("list")
  mylist <- eval(mycall, x)
  for (i in names(mylist)) assign(i, mylist[[i]], parent.frame())
}

Here's an example:
  R> myfunc <- function() list(val1=7, val2=c(5,5))
  R> multi.assign(myfunc(), a=val1, b=val2, d=val1+val2)
  R> a
     [1] 7
  R> b
     [1] 5 5
  R> d
     [1] 12 12
#
I rather object to this sort of solution.  One of the strong points of
the S language is lists which allow items that belong together to be
in a single object.  It's hard to believe that the results of a single call
to a function don't belong together.

R without an accent (or at least with my accent) would attach the list:

res <- myfunc()
attach(res)
summary(a)


Patrick Burns

Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
David Brahm wrote: