Re: Looping thorugh dataframe

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Looping thorugh dataframe

R help mailing list-2
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 <[hidden email]> 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.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Looping thorugh dataframe

Sarah Goslee
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
<[hidden email]> 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 <[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Looping thorugh dataframe

R help mailing list-2
In reply to this post by R help mailing list-2
> library(dplyr, warn.conflicts=FALSE)
> d <- data.frame(Company=c("MATH","IFUL","SSI","MATH","MATH","SSI"), Turnover=c(2,3,5,7,9,11))
> d %>% group_by(Company) %>% summarize(Count=n(), MeanTurnover=mean(Turnover), TotalTurnover=sum(Turnover))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 4
  Company Count MeanTurnover TotalTurnover
  <chr>   <int>        <dbl>         <dbl>
1 IFUL        1            3             3
2 MATH        3            6            18
3 SSI         2            8            16

[The 'override with .groups' comment arose in a recent version of
dplyr.  It is a bit annoying.]

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Jul 22, 2020 at 4:36 PM e-mail ma015k3113 via R-help
<[hidden email]> 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 <[hidden email]> 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.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.