[R] Looping through a dataframe

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Tue Jul 21 23:31:46 CEST 2020


Hi Ahson,
Guessing what your data frame might look like, here are two easy ways:

All_companies<-data.frame(year=c(1970:2015,2000:2015,2010:2015),
 COMPANY_NUMBER=c(rep(1,46),rep(2,16),rep(3,6)),
 COMPANY_NAME=c(rep("IBM",46),rep("AMAZON",16),rep("SPACE-X",6)))
# easy ways
table(All_companies$COMPANY_NAME)
table(All_companies$COMPANY_NUMBER)

I'm too lazy to provide a difficult way.

Jim

On Wed, Jul 22, 2020 at 3:21 AM e-mail ma015k3113 via R-help
<r-help using r-project.org> wrote:
>
> Dear All, I have a dataframe which has a few thousand companies with unique company numbers and names and  each company has data for several years and each year is stored in a separate row.
>
> I want to get a total for the number of years of data for each company. When I loop through the data with the following command  I get a value of ‘1’ rather than a total of the rows for each company
>
> All_companies$count <-0
>
> while All_companies$COMPANY_NAME == All_companies$COMPANY_NAME + 1
>
> + {All_companies$count=All_companies$count+1}
>
> Can you kindly help me on this?
>
> Ahson
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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