Just starting on my journey to learn R and the book I am using is "Using R
for Introductory Statistics"
One of the problems (page 15, 1.10) goes as follows:
The monthly sales fig for 2002 were (2700, 2600, 3050, . . ). Using diff() find
the month with greatest increase from prev month.
I created a msales var:
msales = c(2700, 2600, 3050 . . )
I named them:
names(msales)=c("Jan", Feb", "Mar" . . .)
to find the month with greatest increase from prev month:
max(diff(msales))
I get the numeric value displayed when I execute the above function, but not the
month. cumsum(msales) displays the results under each month, but not max. How
can I get both the month and value displayed?
Thanks
-Sudhakar
ps: no, I am not a student seeking answers to exercises :)
[[alternative HTML version deleted]]
Hi Sudhakar,
Take a look at ?which.max
msales <- c(2700, 2600, 3050)
names(msales) <- c("Jan", "Feb", "Mar")
names(which.max(diff(msales)))
# [1] "Mar"
HTH,
Jorge
On Sat, Oct 30, 2010 at 12:27 PM, Sudhakar Kumar <> wrote:
>
> Just starting on my journey to learn R and the book I am using is
"Using R
> for Introductory Statistics"
>
> One of the problems (page 15, 1.10) goes as follows:
> The monthly sales fig for 2002 were (2700, 2600, 3050, . . ). Using diff()
> find the month with greatest increase from prev month.
>
> I created a msales var:
> msales = c(2700, 2600, 3050 . . )
>
> I named them:
> names(msales)=c("Jan", Feb", "Mar" . . .)
>
> to find the month with greatest increase from prev month:
> max(diff(msales))
>
> I get the numeric value displayed when I execute the above function, but
> not the month. cumsum(msales) displays the results under each month, but
not
> max. How can I get both the month and value displayed?
>
> Thanks
> -Sudhakar
>
> ps: no, I am not a student seeking answers to exercises :)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 Oct 30, 2010, at 12:27 PM, Sudhakar Kumar wrote:> > Just starting on my journey to learn R and the book I am using is > "Using R for Introductory Statistics" > > One of the problems (page 15, 1.10) goes as follows: > The monthly sales fig for 2002 were (2700, 2600, 3050, . . ). Using > diff() find the month with greatest increase from prev month. > > I created a msales var: > msales = c(2700, 2600, 3050 . . ) > > I named them: > names(msales)=c("Jan", Feb", "Mar" . . .)Get in the habit of copy-pasting from the console. This is not valid code.> > to find the month with greatest increase from prev month: > max(diff(msales)) > > I get the numeric value displayed when I execute the above function, > but not the month. cumsum(msales) displays the results under each > month, but not max. How can I get both the month and value displayed? >> diff(msales)[which.max(diff(msales))] Mar 450 -- David Winsemius, MD West Hartford, CT
Possibly Parallel Threads
- Stoch Prog in R
- Password Option for Windows OpenSSH
- [PATCH AUTOSEL 5.7 077/388] scsi: vhost: Notify TCM about the maximum sg entries supported per command
- [PATCH AUTOSEL 5.4 059/266] scsi: vhost: Notify TCM about the maximum sg entries supported per command
- [PATCH] vhost: scsi: notify TCM about the maximum sg entries supported per command.