Quantcast

how to count unique observations by variables

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to count unique observations by variables

Lijiang Guo
Dear R-helpers,

I have a data frame with 3 variables, each record is a unique combination of
the three variables. I would like to count the number of unique values of v3
in each v1, and save it as a new variable v4 in the same data frame.
e.g.
df1
     [v1] [v2] [v3]
[1,] "a"  "C"  "1"
[2,] "b"  "C"  "2"
[3,] "c"  "B"  "3"
[4,] "a"  "B"  "3"
[5,] "b"  "A"  "2"
[6,] "c"  "A"  "1"

In this case, the 4th column would become (2, 1, 2, 2, 1, 2).

Could someone tell me how to do this?

regards,
Lijiang


--

        [[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
|  
Report Content as Inappropriate

Re: how to count unique observations by variables

David Winsemius

On Oct 16, 2008, at 1:27 AM, Lijiang Guo wrote:

> Dear R-helpers,
>
> I have a data frame with 3 variables, each record is a unique  
> combination of
> the three variables. I would like to count the number of unique  
> values of v3
> in each v1, and save it as a new variable v4 in the same data frame.
> e.g.
> df1
>     [v1] [v2] [v3]
> [1,] "a"  "C"  "1"
> [2,] "b"  "C"  "2"
> [3,] "c"  "B"  "3"
> [4,] "a"  "B"  "3"
> [5,] "b"  "A"  "2"
> [6,] "c"  "A"  "1"
>
> In this case, the 4th column would become (2, 1, 2, 2, 1, 2).
 > txt <- '   v1 v2 v3
+  "a"  "C"  "1"
+  "b"  "C"  "2"
+  "c"  "B"  "3"
+  "a"  "B"  "3"
+  "b"  "A"  "2"
+  "c"  "A"  "1"'

df1 <- read.table(textConnection(txt), header=TRUE)

grps <- tapply(df1$v3, df1$v1,FUN=table)

# > sapply(grps,length)
# a b c
# 2 1 2

df1$v4 <- sapply(grps,length)[df1$v1]

df1

>   v1 v2 v3 v4
> 1  a  C  1  2
> 2  b  C  2  1
> 3  c  B  3  2
> 4  a  B  3  2
> 5  b  A  2  1
> 6  c  A  1  2

--
David Winsemius, MD
Heritage Labs

>
> Could someone tell me how to do this?
>
> regards,
> Lijiang
>
>
> --
>
> [[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.

______________________________________________
[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
|  
Report Content as Inappropriate

Re: how to count unique observations by variables

Wincent
In reply to this post by Lijiang Guo
How about this:

> df1=data.frame(v1=c(1,1,2,3,2,4,1))
> df1$v2 <- ave(df1$v1,df1$v1,FUN=length)
> df1
  v1 v2
1  1  3
2  1  3
3  2  2
4  3  1
5  2  2
6  4  1
7  1  3


On Thu, Oct 16, 2008 at 1:27 PM, Lijiang Guo <[hidden email]> wrote:

> Dear R-helpers,
>
> I have a data frame with 3 variables, each record is a unique combination of
> the three variables. I would like to count the number of unique values of v3
> in each v1, and save it as a new variable v4 in the same data frame.
> e.g.
> df1
>     [v1] [v2] [v3]
> [1,] "a"  "C"  "1"
> [2,] "b"  "C"  "2"
> [3,] "c"  "B"  "3"
> [4,] "a"  "B"  "3"
> [5,] "b"  "A"  "2"
> [6,] "c"  "A"  "1"
>
> In this case, the 4th column would become (2, 1, 2, 2, 1, 2).
>
> Could someone tell me how to do this?
>
> regards,
> Lijiang
>
>
> --
>
>        [[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.
>



--
HUANG Ronggui, Wincent
Tel: (00852) 3442 3832
Ph.D. Candidate, CityU of HK
Master of sociology, Fudan University, China
Bachelor of Social Work, Fudan University, China
Personal homepage: http://ronggui.huang.googlepages.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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to count unique observations by variables

Chuck Cleland
In reply to this post by Lijiang Guo
On 10/16/2008 1:27 AM, Lijiang Guo wrote:

> Dear R-helpers,
>
> I have a data frame with 3 variables, each record is a unique combination of
> the three variables. I would like to count the number of unique values of v3
> in each v1, and save it as a new variable v4 in the same data frame.
> e.g.
> df1
>      [v1] [v2] [v3]
> [1,] "a"  "C"  "1"
> [2,] "b"  "C"  "2"
> [3,] "c"  "B"  "3"
> [4,] "a"  "B"  "3"
> [5,] "b"  "A"  "2"
> [6,] "c"  "A"  "1"
>
> In this case, the 4th column would become (2, 1, 2, 2, 1, 2).
>
> Could someone tell me how to do this?

df1 <- data.frame(V1=rep(c('a','b','c'),2),
                  V2=rep(c('C','B','A'),each=2),
                  V3=c(1,2,3,3,2,1))

df1$V4 <- with(df1, ave(V3, V1, FUN = function(x){length(unique(x))}))

df1
  V1 V2 V3 V4
1  a  C  1  2
2  b  C  2  1
3  c  B  3  2
4  a  B  3  2
5  b  A  2  1
6  c  A  1  2

?ave
?unique
?length

> regards,
> Lijiang
>
> --
>
> [[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.

--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

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