[R] Basic loop programming
PIKAL Petr
petr.pikal at precheza.cz
Wed Jan 9 17:16:04 CET 2013
Hi
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Paolo Donatelli
> Sent: Wednesday, January 09, 2013 5:03 PM
> To: r-help at r-project.org
> Subject: [R] Basic loop programming
>
> Hi all,
>
> newbie question: I am trying to set up a very simple loop without
> succeeding.
>
> Let's say I have monthly observation of two variables for a year
> - Sales_2012_01, Sales_2012_02, Sales_2012_03, .... (total sales
> for jan 2012,feb 2012, etc.)
> - Customers_2012_01, Customers_2012_02, .... (total number of
> customers for jan 2012, etc.)
>
> and I want to create new monthly variables in order to compute revenues
> per customers:
>
> Av_revenue_2012_01 = Sales_2012_01 / Customers_2012_01
> Av_revenue_2012_02 = Sales_2012_02 / Customers_2012_02 ...
>
> how can I proceed?
>
>
> In other programming language I used just to write something like for
> (i in list("01","02, ..., "12") { Av_revenue_2012_'i' = Sales_2012_'i'
> / Customers_2012_'i'
> }
>
> but in R it seems not to work like that. Further, and correct me if I
> am wrong, I cannot use simple (i in 1:12) since I have a 0 digit in
> front of the single-digit months.
Hm. Why do you want to do it in R if you prefer other languages? Did you find R by accident or are you prepared to use it in future? If you want to use it, it is just right time to learn some basics.
Anyway, if you have 2 vectors, call them sales and customers, you can just do
av.revenue <- sales/customers
Until you do not provide more info about your data e.g. by at least some of
?head, ?str or preferably ?dput
you hardly get some suitable advice.
Regards
Petr
>
> thanks in advance for your help
>
> ______________________________________________
> 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.
More information about the R-help
mailing list