s-news
[Top] [All Lists]

Re: sapply question

To: Eric yang <yang_eric9@yahoo.com>
Subject: Re: sapply question
From: Patrick Burns <pburns@pburns.seanet.com>
Date: Mon, 13 Mar 2006 16:31:46 +0000
Cc: s-news@wubios.wustl.edu
In-reply-to: <20060313135011.43722.qmail@web33902.mail.mud.yahoo.com>
References: <20060313135011.43722.qmail@web33902.mail.mud.yahoo.com>
User-agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
How about something like:

paste(x, rep(c('is', 'the last'), c(length(x)-1, 1)), 'letter')


Patrick Burns
patrick@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")

Eric yang wrote:

Dear members, I have a rather simple problem which is consequence of me trying to make my programming more efficient. Suppose, for examlpe, I want to produce this [1] "A is letter" "B is letter" "C is letter" "D is letter" [5] "E the last letter"
I can write the following:
x <- LETTERS[1:5] sapply(seq(along=x), function(count, my.letter) paste(my.letter[count], if(count<5) " is" else " the last"," letter" , sep=""), x) Is it possible to write this without specifying two variables in the function call, e.g. can I do the above using sapply(x, function(my.letter), paste(my.letter, ... but how do I do the "if" part of the paste statement. Thanks in advance of any help. Eric

------------------------------------------------------------------------
Yahoo! Mail
Bring photos to life! New PhotoMail <http://pa.yahoo.com/*http://us.rd.yahoo.com/evt=39174/*http://photomail.mail.yahoo.com> makes sharing a breeze.


<Prev in Thread] Current Thread [Next in Thread>