Count cell Count by her frequency

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

Count cell Count by her frequency

Mat
Hello togehter, i have a data.frame, with value like this:

        A       B
1     10-1  aaa
2     10-1  bbb
3     10-1  abc
4     10-2  vvv
5     10-3  ggg

I want now a evaluation, which character is how often in my data.frame. Like this one:
     A         B
1   10-1   3
2   10-2   1
3   10-3   1

How can i do this?

Thank you.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: Count cell Count by her frequency

arun kirshna
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi,
You can use either ?aggregate() or ?ddply() from library(plyr)
dat1<-read.table(text="
      A       B
1     10-1  aaa
2     10-1  bbb
3     10-1  abc
4     10-2  vvv
5     10-3  ggg
",sep="",header=TRUE,stringsAsFactors=FALSE)
 with(dat1,aggregate(B,by=list(A=A),length))

#     A x
#1 10-1 3
#2 10-2 1
#3 10-3 1
library(plyr)
 ddply(dat1,.(A),function(x) length(x[,1]))
  #   A V1
#1 10-1  3
#2 10-2  1
#3 10-3  1
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: Count cell Count by her frequency

Rui Barradas
In reply to this post by Mat
Hello,

Try the following.

dat <- read.table(text = "
  A       B
1     10-1  aaa
2     10-1  bbb
3     10-1  abc
4     10-2  vvv
5     10-3  ggg
", header = TRUE)

tbl <- table(dat$A)
data.frame(tbl)


Hope this helps,

Rui Barradas
Em 10-12-2012 08:50, Mat escreveu:

> Hello togehter, i have a data.frame, with value like this:
>
>          A       B
> 1     10-1  aaa
> 2     10-1  bbb
> 3     10-1  abc
> 4     10-2  vvv
> 5     10-3  ggg
>
> I want now a evaluation, which character is how often in my data.frame. Like
> this one:
>       A         B
> 1   10-1   3
> 2   10-2   1
> 3   10-3   1
>
> How can i do this?
>
> Thank you.
>
> Mat
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Count-cell-Count-by-her-frequency-tp4652650.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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 cell Count by her frequency

David Carlson
In reply to this post by Mat
You request is not completely clear. I am assuming you want to count the
number of different characters in B for each category in A:

> A <- c("10-1", "10-1", "10-1", "10-2", "10-3")
> B <- c("aaa", "bbb", "abc", "vvv", "ggg")
> dta <- data.frame(A, B)
> dta
     A   B
1 10-1 aaa
2 10-1 bbb
3 10-1 abc
4 10-2 vvv
5 10-3 ggg
> a1 <- tapply(dta$B, dta$A, paste0, collapse="")
> a2 <- strsplit(a1, "")
> a3 <- lapply(a2, unique)
> a4 <- sapply(a3, length)
> dtanew <- data.frame(A=names(a4), B=a4, row.names=1:length(a4))
> dtanew
     A B
1 10-1 3
2 10-2 1
3 10-3 1

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Mat
> Sent: Monday, December 10, 2012 2:50 AM
> To: [hidden email]
> Subject: [R] Count cell Count by her frequency
>
> Hello togehter, i have a data.frame, with value like this:
>
>         A       B
> 1     10-1  aaa
> 2     10-1  bbb
> 3     10-1  abc
> 4     10-2  vvv
> 5     10-3  ggg
>
> I want now a evaluation, which character is how often in my data.frame.
> Like
> this one:
>      A         B
> 1   10-1   3
> 2   10-2   1
> 3   10-3   1
>
> How can i do this?
>
> Thank you.
>
> Mat
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Count-cell-
> Count-by-her-frequency-tp4652650.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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 cell Count by her frequency

Felipe Carrillo
And another way:
library(plyr)
ddply(dta,"A",summarise,B=length(B))

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx


From: David L Carlson <[hidden email]>

>To: 'Mat' <[hidden email]>; [hidden email]
>Sent: Monday, December 10, 2012 7:43 AM
>Subject: Re: [R] Count cell Count by her frequency
>
>You request is not completely clear. I am assuming you want to count the
>number of different characters in B for each category in A:
>
>> A <- c("10-1", "10-1", "10-1", "10-2", "10-3")
>> B <- c("aaa", "bbb", "abc", "vvv", "ggg")
>> dta <- data.frame(A, B)
>> dta
>    A  B
>1 10-1 aaa
>2 10-1 bbb
>3 10-1 abc
>4 10-2 vvv
>5 10-3 ggg
>> a1 <- tapply(dta$B, dta$A, paste0, collapse="")
>> a2 <- strsplit(a1, "")
>> a3 <- lapply(a2, unique)
>> a4 <- sapply(a3, length)
>> dtanew <- data.frame(A=names(a4), B=a4, row.names=1:length(a4))
>> dtanew
>    A B
>1 10-1 3
>2 10-2 1
>3 10-3 1
>
>----------------------------------------------
>David L Carlson
>Associate Professor of Anthropology
>Texas A&M University
>College Station, TX 77843-4352
>
>> -----Original Message-----
>> From: [hidden email] [mailto:r-help-bounces@r-
>> project.org] On Behalf Of Mat
>> Sent: Monday, December 10, 2012 2:50 AM
>> To: [hidden email]
>> Subject: [R] Count cell Count by her frequency
>>
>> Hello togehter, i have a data.frame, with value like this:
>>
>>        A      B
>> 1    10-1  aaa
>> 2    10-1  bbb
>> 3    10-1  abc
>> 4    10-2  vvv
>> 5    10-3  ggg
>>
>> I want now a evaluation, which character is how often in my data.frame.
>> Like
>> this one:
>>      A        B
>> 1  10-1  3
>> 2  10-2  1
>> 3  10-3  1
>>
>> How can i do this?
>>
>> Thank you.
>>
>> Mat
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Count-cell-
>> Count-by-her-frequency-tp4652650.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [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.
>
>
>
        [[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.
Mat
Reply | Threaded
Open this post in threaded view
|

Re: Count cell Count by her frequency

Mat
In reply to this post by arun kirshna
Thank you all, a few ideas worked perfectly for me.
I take  "with(dat1,aggregate(B,by=list(A=A),length))"  for my task.

Have a nice day.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: Count cell Count by her frequency

David Carlson
In reply to this post by Felipe Carrillo
And a third way to interpret the question!

Rui – count rows per category in - A = 3 rows, 1 row, 1 row
David – count different letters used in B per category
    in A – 3 (a,b,c),  1 (v),  1 (g)
Felipe - count different strings in B per category - 3 (aaa,
    bbb, abc), 1 (vvv), 1 (ggg)

Interesting that the sample data provide the same answer to all
three questions, but Felipe's interpretation makes more sense
than mine.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


From: Felipe Carrillo [mailto:[hidden email]]
Sent: Monday, December 10, 2012 10:05 AM
To: [hidden email]; 'Mat'; [hidden email]
Subject: Re: [R] Count cell Count by her frequency

And another way:
library(plyr)
ddply(dta,"A",summarise,B=length(B))
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx

From: David L Carlson <[hidden email]>
To: 'Mat' <[hidden email]>; [hidden email]
Sent: Monday, December 10, 2012 7:43 AM
Subject: Re: [R] Count cell Count by her frequency

You request is not completely clear. I am assuming you want to count the
number of different characters in B for each category in A:

> A <- c("10-1", "10-1", "10-1", "10-2", "10-3")
> B <- c("aaa", "bbb", "abc", "vvv", "ggg")
> dta <- data.frame(A, B)
> dta
    A  B
1 10-1 aaa
2 10-1 bbb
3 10-1 abc
4 10-2 vvv
5 10-3 ggg
> a1 <- tapply(dta$B, dta$A, paste0, collapse="")
> a2 <- strsplit(a1, "")
> a3 <- lapply(a2, unique)
> a4 <- sapply(a3, length)
> dtanew <- data.frame(A=names(a4), B=a4, row.names=1:length(a4))
> dtanew
    A B
1 10-1 3
2 10-2 1
3 10-3 1

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Mat
> Sent: Monday, December 10, 2012 2:50 AM
> To: [hidden email]
> Subject: [R] Count cell Count by her frequency
>
> Hello togehter, i have a data.frame, with value like this:
>
>        A      B
> 1    10-1  aaa
> 2    10-1  bbb
> 3    10-1  abc
> 4    10-2  vvv
> 5    10-3  ggg
>
> I want now a evaluation, which character is how often in my data.frame.
> Like
> this one:
>      A        B
> 1  10-1  3
> 2  10-2  1
> 3  10-3  1
>
> How can i do this?
>
> Thank you.
>
> Mat
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Count-cell-
> Count-by-her-frequency-tp4652650.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

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