Conditional Count in R

I have a question that I hope some of you can help me with. I am doing a dissertation on pharmaceuticals and the effect of concurrency. I deal with this in R, having a panel dataset

I need a variable that takes into account the given source product - how many parallelists exist for this given period of time.

Product_ID PI t 1 0 1 1 1 1 1 1 1 1 0 2 1 1 2 1 1 2 1 1 2 1 1 2 2 0 1 2 1 1 2 0 2 2 1 2 2 0 3 2 1 3 2 1 3 2 1 3 

Ideally, I want a new column here, for example, the number of PI products (PI = 1) for the original (PI = 0) at time t. Thus, the output will look like this:

 Product_ID PI t nPIcomp 1 0 1 2 1 1 1 1 1 1 1 0 2 4 1 1 2 1 1 2 1 1 2 1 1 2 2 0 1 1 2 1 1 2 0 2 1 2 1 2 2 0 3 3 2 1 3 2 1 3 2 1 3 

Hope I made my problem clear :)

Thanks in advance, Henrik

+4
source share
3 answers

Something like that?

 x <- read.table(text = "Product_ID PI t 1 0 1 1 1 1 1 1 1 1 0 2 1 1 2 1 1 2 1 1 2 1 1 2 2 0 1 2 1 1 2 0 2 2 1 2 2 0 3 2 1 3 2 1 3 2 1 3", header = TRUE) find.count <- rle(x$PI) count <- find.count$lengths[find.count$values == 1] x[x$PI == 0, "nPIcomp"] <- count Product_ID PI t nPIcomp 1 1 0 1 2 2 1 1 1 NA 3 1 1 1 NA 4 1 0 2 4 5 1 1 2 NA 6 1 1 2 NA 7 1 1 2 NA 8 1 1 2 NA 9 2 0 1 1 10 2 1 1 NA 11 2 0 2 1 12 2 1 2 NA 13 2 0 3 3 14 2 1 3 NA 15 2 1 3 NA 16 2 1 3 NA 
+3
source

I would use ave and your two columns Product_ID and t as grouping variables. Then, in each group, apply a function that returns the sum PI, followed by the corresponding number NA s:

 dat <- transform(dat, nPIcomp = ave(PI, Product_ID, t, FUN = function(z) { n <- sum(z) c(n, rep(NA, n)) })) 

The same idea can be used with the data.table package if your data is large and speed is a problem.

+2
source

The Roman answers give exactly what you want. If you want to summarize the data, that would be convenient using plyr pacakge ( df is what I called your data.frame ) ...

 ddply( df , .(Product_ID , t ) , summarise , nPIcomp = sum(PI) ) # Product_ID t nPIcomp #1 1 1 2 #2 1 2 4 #3 2 1 1 #4 2 2 1 #5 2 3 3 
+1
source

Source: https://habr.com/ru/post/1481352/


All Articles