[R] How to build a matrix of number of appearance?

jim holtman jholtman at gmail.com
Mon Jul 4 19:47:04 CEST 2011


Here is another way:

> xx <- data.frame(P = sample(5, 100, TRUE), M = sample(5, 100, TRUE), id = 1:100)
> require(data.table)
> xx <- data.table(xx)  # convert to data.table
> count <- xx[
+             , list(count = length(id))
+             , by = list(M, P)
+           ]
> str(count)
Classes ‘data.table’ and 'data.frame':  24 obs. of  3 variables:
 $ M    : int  1 1 1 1 1 2 2 2 2 2 ...
 $ P    : int  1 2 3 4 5 1 2 3 4 5 ...
 $ count: int  5 4 3 2 9 3 3 6 3 7 ...
> count
   M P count
   1 1     5
   1 2     4
   1 3     3
   1 4     2
   1 5     9
   2 1     3
   2 2     3
   2 3     6
   2 4     3


On Mon, Jul 4, 2011 at 5:48 AM, UriB <uriblass at gmail.com> wrote:
> I have a matrix of claims at year1 that I get simply by
>
> claims<-read.csv(file="Claims.csv")
> qq1<-claims[claims$Year=="Y1",]
>
> I have MemberID and ProviderID for every claim in qq1 both are integers
>
> An example for the type of questions that I want to answer is
> how many times ProviderID number 345 appears together with MemberID 23 in
> the table qq1
>
> In order to answer these questions for every possible ProviderId and every
> possible MemberID
> I would like to have a matrix that has first column as memberID when every
> memberID in qq1 appears only once and columns that have number of appearance
> of ProviderID==i for every i that has
> sum(qq1$ProviderID==i)>0
>
> My question is if there is a simple way to do it in R
> Thanks in Advance
>
> Uri
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-build-a-matrix-of-number-of-appearance-tp3643248p3643248.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list