Count values in a dataframe with respect to groups

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

Count values in a dataframe with respect to groups

Marcus Drescher
Dear all,

I am looking for a function to count values belonging to a class within a dataframe (and ignore NAs).

grp = c(1,1,1, 1,2, 2,2)
val = c(2,1,5,NA,3,NA,1)

dta = data.frame(grp=grp, val=val)

The result should look like:

grp count
 1    3
 2    2

At the moment, I am trying to find a function for FUN in aggregate, but with no success. Can anybody help me?

Thanks in advance.
Marcus

______________________________________________
[hidden email] mailing list
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: Count values in a dataframe with respect to groups

Henrique Dallazuanna
Try this:

aggregate(val ~ grp, dta, length)


On Fri, Oct 8, 2010 at 11:28 AM, Marcus Drescher <[hidden email]> wrote:

> Dear all,
>
> I am looking for a function to count values belonging to a class within a
> dataframe (and ignore NAs).
>
> grp = c(1,1,1, 1,2, 2,2)
> val = c(2,1,5,NA,3,NA,1)
>
> dta = data.frame(grp=grp, val=val)
>
> The result should look like:
>
> grp count
>  1    3
>  2    2
>
> At the moment, I am trying to find a function for FUN in aggregate, but
> with no success. Can anybody help me?
>
> Thanks in advance.
> Marcus
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
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: Count values in a dataframe with respect to groups

Marcus Drescher
Thanks! It works great.

One more question: how would I have to set the formula if I would have the columns to count (val1 and val2)?


Betreff: Re: [R] Count values in a dataframe with respect to groups

Try this:

aggregate(val ~ grp, dta, length)

On Fri, Oct 8, 2010 at 11:28 AM, Marcus Drescher <[hidden email]> wrote:
Dear all,

I am looking for a function to count values belonging to a class within a dataframe (and ignore NAs).

grp = c(1,1,1, 1,2, 2,2)
val = c(2,1,5,NA,3,NA,1)

dta = data.frame(grp=grp, val=val)

The result should look like:

grp count
 1    3
 2    2

At the moment, I am trying to find a function for FUN in aggregate, but with no success. Can anybody help me?

Thanks in advance.
Marcus

______________________________________________
[hidden email] mailing list
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.



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] mailing list
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: Count values in a dataframe with respect to groups

Thierry Onkelinx
In reply to this post by Marcus Drescher
Have a look at table

table(na.omit(dta)$grp)

Untested!

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
[hidden email]
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
 

> -----Oorspronkelijk bericht-----
> Van: [hidden email]
> [mailto:[hidden email]] Namens Marcus Drescher
> Verzonden: vrijdag 8 oktober 2010 16:29
> Aan: [hidden email]
> Onderwerp: [R] Count values in a dataframe with respect to groups
>
> Dear all,
>
> I am looking for a function to count values belonging to a
> class within a dataframe (and ignore NAs).
>
> grp = c(1,1,1, 1,2, 2,2)
> val = c(2,1,5,NA,3,NA,1)
>
> dta = data.frame(grp=grp, val=val)
>
> The result should look like:
>
> grp count
>  1    3
>  2    2
>
> At the moment, I am trying to find a function for FUN in
> aggregate, but with no success. Can anybody help me?
>
> Thanks in advance.
> Marcus
>
> ______________________________________________
> [hidden email] mailing list
> 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
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: Count values in a dataframe with respect to groups

Marcus Drescher
In reply to this post by Henrique Dallazuanna
I am sorry for the typo, it is about counting two columns an skipping the NAs.

How would I have to set the formula if I would have two columns to count (val1 and val2)?


Betreff: Re: [R] Count values in a dataframe with respect to groups

Try this:

aggregate(val ~ grp, dta, length)

On Fri, Oct 8, 2010 at 11:28 AM, Marcus Drescher <[hidden email]> wrote:
Dear all,

I am looking for a function to count values belonging to a class within a dataframe (and ignore NAs).

grp = c(1,1,1, 1,2, 2,2)
val = c(2,1,5,NA,3,NA,1)

dta = data.frame(grp=grp, val=val)

The result should look like:

grp count
 1    3
 2    2

At the moment, I am trying to find a function for FUN in aggregate, but with no success. Can anybody help me?

Thanks in advance.
Marcus

______________________________________________
[hidden email] mailing list
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.



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] mailing list
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: Count values in a dataframe with respect to groups

Felipe Carrillo
In reply to this post by Marcus Drescher
Here is an option:

grp = c(1,1,1, 1,2, 2,2)
 val = c(2,1,5,NA,3,NA,1)
 dta = data.frame(grp=grp, val=val)
ddply(dta,"grp",summarise,count=length(na.omit(val)))
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA



----- Original Message ----

> From: Marcus Drescher <[hidden email]>
> To: "[hidden email]" <[hidden email]>
> Sent: Fri, October 8, 2010 8:04:40 AM
> Subject: Re: [R] Count values in a dataframe with respect to groups
>
> Thanks! It works great.
>
> One more question: how would I have to set the formula if I would have the
>columns to count (val1 and val2)?
>
>
> Betreff: Re: [R] Count values in a dataframe with respect to groups
>
> Try this:
>
> aggregate(val ~ grp, dta, length)
>
> On Fri, Oct 8, 2010 at 11:28 AM, Marcus Drescher <[hidden email]> wrote:
> Dear all,
>
> I am looking for a function to count values belonging to a class within a
>dataframe (and ignore NAs).
>
> grp = c(1,1,1, 1,2, 2,2)
> val = c(2,1,5,NA,3,NA,1)
>
> dta = data.frame(grp=grp, val=val)
>
> The result should look like:
>
> grp count
>  1    3
>  2    2
>
> At the moment, I am trying to find a function for FUN in aggregate, but with no
>success. Can anybody help me?
>
> Thanks in advance.
> Marcus
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> [hidden email] mailing list
> 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
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: Count values in a dataframe with respect to groups

Henrique Dallazuanna
In reply to this post by Marcus Drescher
Try this:

aggregate(cbind(val, val2) ~ grp, cbind(grp, !is.na(dta[-1])), sum)


On Fri, Oct 8, 2010 at 12:13 PM, Marcus Drescher <[hidden email]> wrote:

> I am sorry for the typo, it is about counting two columns an skipping the
> NAs.
>
> How would I have to set the formula if I would have two columns to count
> (val1 and val2)?
>
>
> Betreff: Re: [R] Count values in a dataframe with respect to groups
>
> Try this:
>
> aggregate(val ~ grp, dta, length)
>
> On Fri, Oct 8, 2010 at 11:28 AM, Marcus Drescher <[hidden email]> wrote:
> Dear all,
>
> I am looking for a function to count values belonging to a class within a
> dataframe (and ignore NAs).
>
> grp = c(1,1,1, 1,2, 2,2)
> val = c(2,1,5,NA,3,NA,1)
>
> dta = data.frame(grp=grp, val=val)
>
> The result should look like:
>
> grp count
>  1    3
>  2    2
>
> At the moment, I am trying to find a function for FUN in aggregate, but
> with no success. Can anybody help me?
>
> Thanks in advance.
> Marcus
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
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.