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]]
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 at 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 at 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]]
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 ) On Tue, Jul 21, 2020 at 10:21 AM e-mail ma015k3113 via R-help < r-help at 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 at 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]]
As Bert says that does not look like R Have a look an these links for some suggestions on asking questions here. http://adv-r.had.co.nz/Reproducibility.html http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example On Tue, 21 Jul 2020 at 13:42, Bert Gunter <bgunter.4567 at 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 ) > > > On Tue, Jul 21, 2020 at 10:21 AM e-mail ma015k3113 via R-help < > r-help at 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 at 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 at 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. >-- John Kane Kingston ON Canada [[alternative HTML version deleted]]
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 at 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 at 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.
David Winsemius
2020-Jul-25 00:53 UTC
[R] Fortune nomination .... Re: Looping through a dataframe
On 7/21/20 2:31 PM, Jim Lemon wrote:> I want to get a total for the number of years of data for each company. When I loop through the data ....After two one liners using `table`:> I'm too lazy to provide a difficult way. > > Jim >