Looping through a dataframe

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

Looping through a dataframe

R help mailing list-2
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]]

______________________________________________
[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 through a dataframe

Roger J. Bos
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 <
[hidden email]> 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]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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 through a dataframe

Bert Gunter-2
In reply to this post by R help mailing list-2
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 <
[hidden email]> 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]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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 through a dataframe

John Kane-3
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 <[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 )
>
>
> On Tue, Jul 21, 2020 at 10:21 AM e-mail ma015k3113 via R-help <
> [hidden email]> 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]]
> >
> > ______________________________________________
> > [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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>


--
John Kane
Kingston ON Canada

        [[alternative HTML version deleted]]

______________________________________________
[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 through a dataframe

John Kane-3
It occurs to me a simple table command will do what you say you want but I
suspect the real analysis is more complicated

dat1  <-  data.frame(aa =  sample(letters[1:5], 10, replace = TRUE),
           bb  =  1:10)

 table(dat1$aa)

On Tue, 21 Jul 2020 at 14:01, John Kane <[hidden email]> wrote:

> 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 <[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 )
>>
>>
>> On Tue, Jul 21, 2020 at 10:21 AM e-mail ma015k3113 via R-help <
>> [hidden email]> 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]]
>> >
>> > ______________________________________________
>> > [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.
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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.
>>
>
>
> --
> John Kane
> Kingston ON Canada
>


--
John Kane
Kingston ON Canada

        [[alternative HTML version deleted]]

______________________________________________
[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 through a dataframe

Jim Lemon-4
In reply to this post by R help mailing list-2
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
<[hidden email]> 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]]
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Fortune nomination .... Re: Looping through a dataframe

David Winsemius

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
>

______________________________________________
[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: Fortune nomination .... Re: Looping through a dataframe

John Kane-3
Yes I think so.

On Fri, 24 Jul 2020 at 20:53, David Winsemius <[hidden email]>
wrote:

>
> 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
> >
>
> ______________________________________________
> [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.
>


--
John Kane
Kingston ON Canada

        [[alternative HTML version deleted]]

______________________________________________
[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: Fortune nomination .... Re: Looping through a dataframe

Chris Evans
I really don't want to put too many Emails to the list but I had the same reaction to that lovely line ... so now my question is:

Is there a way to make fortune nominations other than through the list.  I see the package has a long list of illustrious authors but I can't find a side channel or open port for nominations despite a bit of searching.

TIA and very best to all,

Chris

----- Original Message -----
> From: "John Kane" <[hidden email]>
> To: "David Winsemius" <[hidden email]>
> Cc: "R. Help Mailing List" <[hidden email]>
> Sent: Saturday, 25 July, 2020 02:55:34
> Subject: Re: [R] Fortune nomination .... Re: Looping through a dataframe

> Yes I think so.
>
> On Fri, 24 Jul 2020 at 20:53, David Winsemius <[hidden email]>
> wrote:
>
>>
>> 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
>> >
>>
>> ______________________________________________
>> [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.
>>
>
>
> --
> John Kane
> Kingston ON Canada
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.

--
Small contribution in our coronavirus rigours:
https://www.coresystemtrust.org.uk/home/free-options-to-replace-paper-core-forms-during-the-coronavirus-pandemic/

Chris Evans <[hidden email]> Visiting Professor, University of Sheffield <[hidden email]>
I do some consultation work for the University of Roehampton <[hidden email]> and other places
but <[hidden email]> remains my main Email address.  I have a work web site at:
   https://www.psyctc.org/psyctc/
and a site I manage for CORE and CORE system trust at:
   http://www.coresystemtrust.org.uk/
I have "semigrated" to France, see:
   https://www.psyctc.org/pelerinage2016/semigrating-to-france/ 
   https://www.psyctc.org/pelerinage2016/register-to-get-updates-from-pelerinage2016/

If you want an Emeeting, I am trying to keep them to Thursdays and my diary is at:
   https://www.psyctc.org/pelerinage2016/ceworkdiary/
Beware: French time, generally an hour ahead of UK.

______________________________________________
[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: Fortune nomination .... Re: Looping through a dataframe

Michael Dewey-3
Dear Chris

Just send it to the maintainer Achim Zeileis to whom I have cc'ed this
in case he is not reading the list at the moment.

Michael

On 25/07/2020 06:21, Chris Evans wrote:

> I really don't want to put too many Emails to the list but I had the same reaction to that lovely line ... so now my question is:
>
> Is there a way to make fortune nominations other than through the list.  I see the package has a long list of illustrious authors but I can't find a side channel or open port for nominations despite a bit of searching.
>
> TIA and very best to all,
>
> Chris
>
> ----- Original Message -----
>> From: "John Kane" <[hidden email]>
>> To: "David Winsemius" <[hidden email]>
>> Cc: "R. Help Mailing List" <[hidden email]>
>> Sent: Saturday, 25 July, 2020 02:55:34
>> Subject: Re: [R] Fortune nomination .... Re: Looping through a dataframe
>
>> Yes I think so.
>>
>> On Fri, 24 Jul 2020 at 20:53, David Winsemius <[hidden email]>
>> wrote:
>>
>>>
>>> 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
>>>>
>>>
>>> ______________________________________________
>>> [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.
>>>
>>
>>
>> --
>> John Kane
>> Kingston ON Canada
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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.
>

--
Michael
http://www.dewey.myzen.co.uk/home.html

______________________________________________
[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.