What you are asking is one area where the package data.table really shines. You didn't provide an example, but based on your question you would do something like:
library(data.table) dt <- as.data.table(All_companies) dt[, .N, by=COMPANY_NAME] You will have to read up on data.table, but .N gives you the number of observations and when using data.table (unike data.frames) you can use the column name directly in the 'by' parameter with needing to append the name of the R object or use quotes. Obviously this is just one of many ways to do what you are asking. HTH, Roger On Tue, Jul 21, 2020 at 1:21 PM e-mail ma015k3113 via R-help < r-help@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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.