[R] Looping thorugh dataframe

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Thu Jul 23 01:53:53 CEST 2020


Hi,

Your sample code suggests that you don't yet understand how R works,
and might benefit from a tutorial or two. However, your verbal
description of what you want is quite straightforward. Here's a
R-style way to count the number of times each company appears, and to
get the mean value of Turnover for each company:


All_companies <- read.table(text =
"COMPANY_NUMBER  COMPANY_NAME    YEAR_END_DATE   Turnover
22705   AA      30/09/10        420000
22705   AA      30/09/09        406000
113560  BB      30/06/19        474000
192761  CC      31/01/19        796000
192761  CC      31/01/18        909000
192761  CC      31/01/17        788000
5625107         DD      30/06/19        3254002
5625107         DD      30/06/18        1840436", header=TRUE)

table(All_companies$COMPANY_NAME)

AA BB CC DD
 2  1  3  2

aggregate(Turnover ~ COMPANY_NAME, data = All_companies, FUN = mean)

  COMPANY_NAME Turnover
1           AA   413000
2           BB   474000
3           CC   831000
4           DD  2547219


On Wed, Jul 22, 2020 at 7:36 PM e-mail ma015k3113 via R-help
<r-help using r-project.org> wrote:
>
> Bert, thanks for responding to my email. I do realise that newbie's like my can expect curt answers but not to worry. I am definitely learning 'R' and what I posted are also statements from R. The statements run perfectly well but don't do what I want them to do. My mistake I have posted sample data. Here is the data:
>
> COMPANY_NUMBER  COMPANY_NAME    YEAR_END_DATE   Turnover
> 22705   AA      30/09/10        420,000
> 22705   AA      30/09/09        406,000
> 113560  BB      30/06/19        474,000
> 192761  CC      31/01/19        796,000
> 192761  CC      31/01/18        909,000
> 192761  CC      31/01/17        788,000
> 5625107         DD      30/06/19        3,254,002
> 5625107         DD      30/06/18        1,840,436
>
> All_companies$count <-0
> while All_companies$COMPANY_NAME == All_companies$COMPANY_NAME + 1
> + {All_companies$count=All_companies$count+1}
>
> I want to find out many times each company has appeared in the dataframe and the average of the turnover for the years. Like company AA appears twice and average turnover is 413,000.
>
> 'All_companies' is the name of the dataframe.
>
> In the end apologies for not being more clear the first time around and of course many thanks for your help in advance.
>
> Kind regards
>
>
> Ahson
>
> On 21 July 2020 at 18:41 Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> What language are you programming in? -- it certainly isn't R.
>
> I suggest that you stop what you're doing and go through an R tutorial or two before proceeding. This list cannot serve as a substitute for doing such homework (is this homework, btw? -- that's off topic here) nor can we provide such tutorials.
>
> I'm pretty sure the answer is quite simple, though it's a bit unclear as you did not provide a reprex (see the posting guide linked below for how to post here). However, I see no purpose in my blurting it out when you do not seem aware of even the most basic R constructs -- e.g. see ?while. Of course, others may disagree and provide you what you seek.
>


-- 
Sarah Goslee (she/her)
http://www.numberwright.com



More information about the R-help mailing list