[R] Looping thorugh dataframe

William Dunlap wdun|@p @end|ng |rom t|bco@com
Thu Jul 23 02:38:26 CEST 2020


> 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
<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.
>
>
> 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 )
>
> ______________________________________________
> R-help using r-project.org 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.



More information about the R-help mailing list