Aggregate column intervals into new columns in data.table - r
I would like to aggregate a data.table based on intervals of a column (time). The idea here is that each interval should be a separate column with a different name in the output.
I've seen a similar question in SO but I couldn't get my head around the problem. help?
reproducible example
library(data.table)
# sample data
set.seed(1L)
dt <- data.table( id= sample(LETTERS,50,replace=TRUE),
time= sample(60,50,replace=TRUE),
points= sample(1000,50,replace=TRUE))
# simple summary by `id`
dt[, .(total = sum(points)), by=id]
> id total
> 1: J 2058
> 2: T 1427
> 3: C 1020
In the desired output, each column would be named after the interval size they originate from. For example with three intervals, say time < 10, time < 20, time < 30, the head of the output should be:
id | total | subtotal_under10 | subtotal_under20 | subtotal_under30
Exclusive Subtotal Categories
set.seed(1L);
N <- 50L;
dt <- data.table(id=sample(LETTERS,N,T),time=sample(60L,N,T),points=sample(1000L,N,T));
breaks <- seq(0L,as.integer(ceiling((max(dt$time)+1L)/10)*10),10L);
cuts <- cut(dt$time,breaks,labels=paste0('subtotal_under',breaks[-1L]),right=F);
res <- dcast(dt[,.(subtotal=sum(points)),.(id,cut=cuts)],id~cut,value.var='subtotal');
res <- res[dt[,.(total=sum(points)),id]][order(id)];
res;
## id subtotal_under10 subtotal_under20 subtotal_under30 subtotal_under40 subtotal_under50 subtotal_under60 total
## 1: A NA NA 176 NA NA 512 688
## 2: B NA NA 599 NA NA NA 599
## 3: C 527 NA NA NA NA NA 527
## 4: D NA NA 174 NA NA NA 174
## 5: E NA 732 643 NA NA NA 1375
## 6: F 634 NA NA NA NA 1473 2107
## 7: G NA NA 1410 NA NA NA 1410
## 8: I NA NA NA NA NA 596 596
## 9: J 447 NA 640 NA NA 354 1441
## 10: K 508 NA NA NA NA 454 962
## 11: M NA 14 1358 NA NA NA 1372
## 12: N NA NA NA NA 730 NA 730
## 13: O NA NA 271 NA NA 259 530
## 14: P NA NA NA NA 78 NA 78
## 15: Q 602 NA 485 NA 925 NA 2012
## 16: R NA 599 357 479 NA NA 1435
## 17: S NA 986 716 865 NA NA 2567
## 18: T NA NA NA NA 105 NA 105
## 19: U NA NA NA 239 1163 641 2043
## 20: V NA 683 NA NA 929 NA 1612
## 21: W NA NA NA NA 229 NA 229
## 22: X 214 993 NA NA NA NA 1207
## 23: Y NA 130 992 NA NA NA 1122
## 24: Z NA NA NA NA 104 NA 104
## id subtotal_under10 subtotal_under20 subtotal_under30 subtotal_under40 subtotal_under50 subtotal_under60 total
Cumulative Subtotal Categories
I've come up with a new solution based on the requirement of cumulative subtotals.
My objective was to avoid looping operations such as lapply(), since I realized that it should be possible to compute the desired result using only vectorized operations such as findInterval(), vectorized/cumulative operations such as cumsum(), and vector indexing.
I succeeded, but I should warn you that the algorithm is fairly intricate, in terms of its logic. I'll try to explain it below.
breaks <- seq(0L,as.integer(ceiling((max(dt$time)+1L)/10)*10),10L);
ints <- findInterval(dt$time,breaks);
res <- dt[,{ y <- ints[.I]; o <- order(y); y <- y[o]; w <- which(c(y[-length(y)]!=y[-1L],T)); v <- rep(c(NA,w),diff(c(1L,y[w],length(breaks)))); c(sum(points),as.list(cumsum(points[o])[v])); },id][order(id)];
setnames(res,2:ncol(res),c('total',paste0('subtotal_under',breaks[-1L])));
res;
## id total subtotal_under10 subtotal_under20 subtotal_under30 subtotal_under40 subtotal_under50 subtotal_under60
## 1: A 688 NA NA 176 176 176 688
## 2: B 599 NA NA 599 599 599 599
## 3: C 527 527 527 527 527 527 527
## 4: D 174 NA NA 174 174 174 174
## 5: E 1375 NA 732 1375 1375 1375 1375
## 6: F 2107 634 634 634 634 634 2107
## 7: G 1410 NA NA 1410 1410 1410 1410
## 8: I 596 NA NA NA NA NA 596
## 9: J 1441 447 447 1087 1087 1087 1441
## 10: K 962 508 508 508 508 508 962
## 11: M 1372 NA 14 1372 1372 1372 1372
## 12: N 730 NA NA NA NA 730 730
## 13: O 530 NA NA 271 271 271 530
## 14: P 78 NA NA NA NA 78 78
## 15: Q 2012 602 602 1087 1087 2012 2012
## 16: R 1435 NA 599 956 1435 1435 1435
## 17: S 2567 NA 986 1702 2567 2567 2567
## 18: T 105 NA NA NA NA 105 105
## 19: U 2043 NA NA NA 239 1402 2043
## 20: V 1612 NA 683 683 683 1612 1612
## 21: W 229 NA NA NA NA 229 229
## 22: X 1207 214 1207 1207 1207 1207 1207
## 23: Y 1122 NA 130 1122 1122 1122 1122
## 24: Z 104 NA NA NA NA 104 104
## id total subtotal_under10 subtotal_under20 subtotal_under30 subtotal_under40 subtotal_under50 subtotal_under60
Explanation
breaks <- seq(0L,as.integer(ceiling((max(dt$time)+1L)/10)*10),10L);
breaks <- seq(0,ceiling(max(dt$time)/10)*10,10); ## old derivation, for reference
First, we derive breaks as before. I should mention that I realized there was a subtle bug in my original derivation algorithm. Namely, if the maximum time value is a multiple of 10, then the derived breaks vector would've been short by 1. Consider if we had a maximum time value of 60. The original calculation of the upper limit of the sequence would've been ceiling(60/10)*10, which is just 60 again. But it should be 70, since the value 60 technically belongs in the 60 <= time < 70 interval. I fixed this in the new code (and retroactively amended the old code) by adding 1 to the maximum time value when computing the upper limit of the sequence. I also changed two of the literals to integers and added an as.integer() coercion to preserve integerness.
ints <- findInterval(dt$time,breaks);
Second, we precompute the interval indexes into which each time value falls. We can precompute this once for the entire table, because we'll be able to index out each id group's subset within the j argument of the subsequent data.table indexing operation. Note that findInterval() behaves perfectly for our purposes using the default arguments; we don't need to mess with rightmost.closed, all.inside, or left.open. This is because findInterval() by default uses lower <= value < upper logic, and it's impossible for values to fall below the lowest break (which is zero) or on or above the highest break (which must be greater than the maximum time value because of the way we derived it).
res <- dt[,{ y <- ints[.I]; o <- order(y); y <- y[o]; w <- which(c(y[-length(y)]!=y[-1L],T)); v <- rep(c(NA,w),diff(c(1L,y[w],length(breaks)))); c(sum(points),as.list(cumsum(points[o])[v])); },id][order(id)];
Third, we compute the aggregation using a data.table indexing operation, grouping by id. (Afterward we sort by id using a chained indexing operation, but that's not significant.) The j argument consists of 6 statements executed in a braced block which I will now explain one at a time.
y <- ints[.I];
This pulls out the interval indexes for the current id group in input order.
o <- order(y);
This captures the order of the group's records by interval. We will need this order for the cumulative summation of points, as well as the derivation of which indexes in that cumulative sum represent the desired interval subtotals. Note that the within-interval orders (i.e. ties) are irrelevant, since we're only going to extract the final subtotals of each interval, which will be the same regardless if and how order() breaks ties.
y <- y[o];
This actually reorders y to interval order.
w <- which(c(y[-length(y)]!=y[-1L],T));
This computes the endpoints of each interval sequence, IOW the indexes of only those elements that comprise the final element of an interval. This vector will always contain at least one index, it will never contain more indexes than there are intervals, and it will be unique.
v <- rep(c(NA,w),diff(c(1L,y[w],length(breaks))));
This repeats each element of w according to its distance (as measured in intervals) from its following element. We use diff() on y[w] to compute these distances, requiring an appended length(breaks) element to properly treat the final element of w. We also need to cover if the first interval (and zero or more subsequent intervals) is not represented in the group, in which case we must pad it with NAs. This requires prepending an NA to w and prepending a 1 to the argument vector to diff().
c(sum(points),as.list(cumsum(points[o])[v]));
Finally, we can compute the group aggregation result. Since you want a total column and then separate subtotal columns, we need a list starting with the total aggregation, followed by one list component per subtotal value. points[o] gives us the target summation operand in interval order, which we then cumulatively sum, and then index with v to produce the correct sequence of cumulative subtotals. We must coerce the vector to a list using as.list(), and then prepend the list with the total aggregation, which is simply the sum of the entire points vector. The resulting list is then returned from the j expression.
setnames(res,2:ncol(res),c('total',paste0('subtotal_under',breaks[-1L])));
Last, we set the column names. It is more performant to set them once after-the-fact, as opposed to having them set repeatedly in the j expression.
Benchmarking
For benchmarking, I wrapped my code in a function, and did the same for Mike's code. I decided to make my breaks variable a parameter with its derivation as the default argument, and I did the same for Mike's my_nums variable, but without a default argument.
Also note that for the identical() proofs-of-equivalence, I coerce the two results to matrix, because Mike's code always computes the total and subtotal columns as doubles, whereas my code preserves the type of the input points column (i.e. integer if it was integer, double if it was double). Coercing to matrix was the easiest way I could think of to verify that the actual data is equivalent.
library(data.table);
library(microbenchmark);
bgoldst <- function(dt,breaks=seq(0L,as.integer(ceiling((max(dt$time)+1L)/10)*10),10L)) { ints <- findInterval(dt$time,breaks); res <- dt[,{ y <- ints[.I]; o <- order(y); y <- y[o]; w <- which(c(y[-length(y)]!=y[-1L],T)); v <- rep(c(NA,w),diff(c(1L,y[w],length(breaks)))); c(sum(points),as.list(cumsum(points[o])[v])); },id][order(id)]; setnames(res,2:ncol(res),c('total',paste0('subtotal_under',breaks[-1L]))); res; };
mike <- function(dt,my_nums) { cols <- sapply(1:length(my_nums),function(x){return(paste0("subtotal_under",my_nums[x]))}); dt[,(cols) := lapply(my_nums,function(x) ifelse(time<x,points,NA))]; dt[,total := points]; dt[,lapply(.SD,function(x){ if (all(is.na(x))){ as.numeric(NA) } else{ as.numeric(sum(x,na.rm=TRUE)) } }),by=id, .SDcols=c("total",cols) ][order(id)]; };
## OP's sample input
set.seed(1L);
N <- 50L;
dt <- data.table(id=sample(LETTERS,N,T),time=sample(60L,N,T),points=sample(1000L,N,T));
identical(as.matrix(bgoldst(copy(dt))),as.matrix(mike(copy(dt),c(10,20,30,40,50,60))));
## [1] TRUE
microbenchmark(bgoldst(copy(dt)),mike(copy(dt),c(10,20,30,40,50,60)));
## Unit: milliseconds
## expr min lq mean median uq max neval
## bgoldst(copy(dt)) 3.281380 3.484301 3.793532 3.588221 3.780023 6.322846 100
## mike(copy(dt), c(10, 20, 30, 40, 50, 60)) 3.243746 3.442819 3.731326 3.526425 3.702832 5.618502 100
Mike's code is actually faster (usually) by a small amount for the OP's sample input.
## large input 1
set.seed(1L);
N <- 1e5L;
dt <- data.table(id=sample(LETTERS,N,T),time=sample(60L,N,T),points=sample(1000L,N,T));
identical(as.matrix(bgoldst(copy(dt))),as.matrix(mike(copy(dt),c(10,20,30,40,50,60,70))));
## [1] TRUE
microbenchmark(bgoldst(copy(dt)),mike(copy(dt),c(10,20,30,40,50,60,70)));
## Unit: milliseconds
## expr min lq mean median uq max neval
## bgoldst(copy(dt)) 19.44409 19.96711 22.26597 20.36012 21.26289 62.37914 100
## mike(copy(dt), c(10, 20, 30, 40, 50, 60, 70)) 94.35002 96.50347 101.06882 97.71544 100.07052 146.65323 100
For this much larger input, my code significantly outperforms Mike's.
In case you're wondering why I had to add the 70 to Mike's my_nums argument, it's because with so many more records, the probability of getting a 60 in the random generation of dt$time is extremely high, which requires the additional interval. You can see that the identical() call gives TRUE, so this is correct.
## large input 2
set.seed(1L);
N <- 1e6L;
dt <- data.table(id=sample(LETTERS,N,T),time=sample(60L,N,T),points=sample(1000L,N,T));
identical(as.matrix(bgoldst(copy(dt))),as.matrix(mike(copy(dt),c(10,20,30,40,50,60,70))));
## [1] TRUE
microbenchmark(bgoldst(copy(dt)),mike(copy(dt),c(10,20,30,40,50,60,70)));
## Unit: milliseconds
## expr min lq mean median uq max neval
## bgoldst(copy(dt)) 204.8841 207.2305 225.0254 210.6545 249.5497 312.0077 100
## mike(copy(dt), c(10, 20, 30, 40, 50, 60, 70)) 1039.4480 1086.3435 1125.8285 1116.2700 1158.4772 1412.6840 100
For this even larger input, the performance difference is slightly more pronounced.
I'm pretty sure something like this might work as well:
# sample data
set.seed(1)
dt <- data.table( id= sample(LETTERS,50,replace=TRUE),
time= sample(60,50,replace=TRUE),
points= sample(1000,50,replace=TRUE))
#Input numbers
my_nums <- c(10,20,30)
#Defining columns
cols <- sapply(1:length(my_nums),function(x){return(paste0("subtotal_under",my_nums[x]))})
dt[,(cols) := lapply(my_nums,function(x) ifelse(time<x,points,NA))]
dt[,total := sum((points)),by=id]
dt[,(cols):= lapply(.SD,sum,na.rm=TRUE),by=id, .SDcols=cols ]
head(dt)
id time points subtotal_under10 subtotal_under20 subtotal_under30 total
1: G 29 655 0 0 1410 1410
2: J 52 354 447 447 1087 1441
3: O 27 271 0 0 271 530
4: X 15 993 214 1207 1207 1207
5: F 5 634 634 634 634 2107
6: X 6 214 214 1207 1207 1207
Edit: To aggregate columns, you can simply change to:
#Defining columns
cols <- sapply(1:length(my_nums),function(x){return(paste0("subtotal_under",my_nums[x]))})
dt[,(cols) := lapply(my_nums,function(x) ifelse(time<x,points,NA))]
dt[,total := points]
dt[,lapply(.SD,function(x){
if (all(is.na(x))){
as.numeric(NA)
} else{
as.numeric(sum(x,na.rm=TRUE))
}
}),by=id, .SDcols=c("total",cols) ]
This should give the expected output of 1 row per ID.
Edit: Per OPs comment below, changed so that 0s are NA. Changed so don't need an as.numeric() call in the building of columns.
After a while thinking about this, I think I've arrived at a very simple and fast solution based on conditional sum ! The small problem is that I haven't figured out how to automate this code to create a larger number of columns without having to write each of them. Any help here would be really welcomed !
library(data.table)
dt[, .( total = sum(points)
, subtotal_under10 = sum(points[which( time < 10)])
, subtotal_under20 = sum(points[which( time < 20)])
, subtotal_under30 = sum(points[which( time < 30)])
, subtotal_under40 = sum(points[which( time < 40)])
, subtotal_under50 = sum(points[which( time < 50)])
, subtotal_under60 = sum(points[which( time < 60)])), by=id][order(id)]
microbenchmark
Using the same benchmark proposed by #bgoldst in another answer, this simple solution is much faster than the alternatives:
set.seed(1L)
N <- 1e6L
dt <- data.table(id=sample(LETTERS,N,T),time=sample(60L,N,T),points=sample(1000L,N,T))
library(microbenchmark)
microbenchmark(rafa(copy(dt)),bgoldst(copy(dt)),mike(copy(dt),c(10,20,30,40,50,60)))
# expr min lq mean median uq max neval cld
# rafa(copy(dt)) 95.79 102.45 117.25 110.09 116.95 278.50 100 a
# bgoldst(copy(dt)) 192.53 201.85 211.04 207.50 213.26 354.17 100 b
# mike(copy(dt), c(10, 20, 30, 40, 50, 60)) 844.80 890.53 955.29 921.27 1041.96 1112.18 100 c
Related
How to effectively determine the maximum difference between the variable value in each row and same variable subsequent row values in data.table in R
What is the most efficient way to determine the maximum positive difference between the value (X) for each row and the subsequent values of the same variable (X) within group (Y) in data.table in R. Example: set.seed(1) dt <- data.table(X = sample(100:200, 500455, replace = TRUE), Y = unlist(sapply(10:1000, function(x) rep(x, x)))) Here's my solution which I consider ineffective and slow: dt[, max_diff := vapply(1:.N, function(x) max(X[x:.N] - X[x]), numeric(1)), by = Y] head(dt, 21) X Y max_diff 1: 126 10 69 2: 137 10 58 3: 157 10 38 4: 191 10 4 5: 120 10 75 6: 190 10 5 7: 195 10 0 8: 166 10 0 9: 163 10 0 10: 106 10 0 11: 120 11 80 12: 117 11 83 13: 169 11 31 14: 138 11 62 15: 177 11 23 16: 150 11 50 17: 172 11 28 18: 200 11 0 19: 138 11 56 20: 178 11 16 21: 194 11 0 If you can advise the efficient (faster) solution?
Here's a dplyr solution that is about 20x faster and gets the same results. I presume the data.table equivalent would be yet faster. (EDIT: see bottom - it is!) The speedup comes from reducing how many comparisons need to be performed. The largest difference will always be found against the largest remaining number in the group, so it's faster to identify that number first and do only the one subtraction per row. First, the original solution takes about 4 sec on my machine: tictoc::tic("OP data.table") dt[, max_diff := vapply(1:.N, function(x) max(X[x:.N] - X[x]), numeric(1)), by = Y] tictoc::toc() # OP data.table: 4.594 sec elapsed But in only 0.2 sec we can take that data.table, convert to a data frame, add the orig_row row number, group by Y, reverse sort by orig_row, take the difference between X and the cumulative max of X, ungroup, and rearrange in original order: library(dplyr) tictoc::tic("dplyr") dt2 <- dt %>% as_data_frame() %>% mutate(orig_row = row_number()) %>% group_by(Y) %>% arrange(-orig_row) %>% mutate(max_diff2 = cummax(X) - X) %>% ungroup() %>% arrange(orig_row) tictoc::toc() # dplyr: 0.166 sec elapsed all.equal(dt2$max_diff, dt2$max_diff2) #[1] TRUE EDIT: as #david-arenburg suggests in the comments, this can be done lightning-fast in data.table with an elegant line: dt[.N:1, max_diff2 := cummax(X) - X, by = Y] On my computer, that's about 2-4x faster than the dplyr solution above.
R: sum vector by vector of conditions
I am trying to obtain a vector, which contains sum of elements which fit condition. values = runif(5000) bin = seq(0, 0.9, by = 0.1) sum(values < bin) I expected that sum will return me 10 values - a sum of "values" elements which fit "<" condition per each "bin" element. However, it returns only one value. How can I achieve the result without using a while loop?
I understand this to mean that you want, for each value in bin, the number of elements in values that are less than bin. So I think you want vapply() here vapply(bin, function(x) sum(values < x), 1L) # [1] 0 497 1025 1501 1981 2461 2955 3446 3981 4526 If you want a little table for reference, you could add names v <- vapply(bin, function(x) sum(values < x), 1L) setNames(v, bin) # 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 # 0 497 1025 1501 1981 2461 2955 3446 3981 4526
I personally prefer data.table over tapply or vapply, and findInterval over cut. set.seed(1) library(data.table) dt <- data.table(values, groups=findInterval(values, bin)) setkey(dt, groups) dt[,.(n=.N, v=sum(values)), groups][,list(cumsum(n), cumsum(v)),] # V1 V2 # 1: 537 26.43445 # 2: 1041 101.55686 # 3: 1537 226.12625 # 4: 2059 410.41487 # 5: 2564 637.18782 # 6: 3050 904.65876 # 7: 3473 1180.53342 # 8: 3951 1540.18559 # 9: 4464 1976.23067 #10: 5000 2485.44920 cbind(vapply(bin, function(x) sum(values < x), 1L)[-1], cumsum(tapply( values, cut(values, bin), sum))) # [,1] [,2] #(0,0.1] 537 26.43445 #(0.1,0.2] 1041 101.55686 #(0.2,0.3] 1537 226.12625 #(0.3,0.4] 2059 410.41487 #(0.4,0.5] 2564 637.18782 #(0.5,0.6] 3050 904.65876 #(0.6,0.7] 3473 1180.53342 #(0.7,0.8] 3951 1540.18559 #(0.8,0.9] 4464 1976.23067
Using tapply with a cut()-constructed INDEX vector seems to deliver: tapply( values, cut(values, bin), sum) (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6] 25.43052 71.06897 129.99698 167.56887 222.74620 277.16395 (0.6,0.7] (0.7,0.8] (0.8,0.9] 332.18292 368.49341 435.01104 Although I'm guessing you would want the cut-vector to extend to 1.0: bin = seq(0, 1, by = 0.1) tapply( values, cut(values, bin), sum) (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6] 25.48087 69.87902 129.37348 169.46013 224.81064 282.22455 (0.6,0.7] (0.7,0.8] (0.8,0.9] (0.9,1] 335.43991 371.60885 425.66550 463.37312 I see that I understood the question differently than Richard. If you wanted his result you can use cumsum on my result.
Using dplyr: set.seed(1) library(dplyr) df %>% group_by(groups) %>% summarise(count = n(), sum = sum(values)) %>% mutate(cumcount= cumsum(count), cumsum = cumsum(sum)) Output: groups count sum cumcount cumsum 1 (0,0.1] 537 26.43445 537 26.43445 2 (0.1,0.2] 504 75.12241 1041 101.55686 3 (0.2,0.3] 496 124.56939 1537 226.12625 4 (0.3,0.4] 522 184.28862 2059 410.41487 5 (0.4,0.5] 505 226.77295 2564 637.18782 6 (0.5,0.6] 486 267.47094 3050 904.65876 7 (0.6,0.7] 423 275.87466 3473 1180.53342 8 (0.7,0.8] 478 359.65217 3951 1540.18559 9 (0.8,0.9] 513 436.04508 4464 1976.23067 10 NA 536 509.21853 5000 2485.44920
Filter rows based on values of multiple columns in R
Here is the data set, say name is DS. Abc Def Ghi 1 41 190 67 2 36 118 72 3 12 149 74 4 18 313 62 5 NA NA 56 6 28 NA 66 7 23 299 65 8 19 99 59 9 8 19 61 10 NA 194 69 How to get a new dataset DSS where value of column Abc is greater than 25, and value of column Def is greater than 100.It should also ignore any row if value of atleast one column in NA. I have tried few options but wasn't successful. Your help is appreciated.
There are multiple ways of doing it. I have given 5 methods, and the first 4 methods are faster than the subset function. R Code: # Method 1: DS_Filtered <- na.omit(DS[(DS$Abc > 20 & DS$Def > 100), ]) # Method 2: which function also ignores NA DS_Filtered <- DS[ which( DS$Abc > 20 & DS$Def > 100) , ] # Method 3: DS_Filtered <- na.omit(DS[(DS$Abc > 20) & (DS$Def >100), ]) # Method 4: using dplyr package DS_Filtered <- filter(DS, DS$Abc > 20, DS$Def >100) DS_Filtered <- DS %>% filter(DS$Abc > 20 & DS$Def >100) # Method 5: Subset function by default ignores NA DS_Filtered <- subset(DS, DS$Abc >20 & DS$Def > 100)
R - setting equiprobability over a specific variable when sampling
I have a data set with more than 2 millions entries which I load into a data frame. I'm trying to grab a subset of the data. I need around 10000 entries but I need the entries to be picked with equal probability on one variable. This is what my data looks like with str(data): 'data.frame': 2685628 obs. of 3 variables: $ category : num 3289 3289 3289 3289 3289 ... $ id: num 8064180 8990447 747922 9725245 9833082 ... $ text : chr "text1" "text2" "text3" "text4" ... You've noticed that I have 3 variables : category,id and text. I have tried the following : > sample_data <- data[sample(nrow(data),10000,replace=FALSE),] Of course this works, but the probability of sample if not equal. Here is the output of count(sample_data$category) : x freq 1 3289 707 2 3401 341 3 3482 160 4 3502 243 5 3601 1513 6 3783 716 7 4029 423 8 4166 21 9 4178 894 10 4785 31 11 5108 121 12 5245 2178 13 5637 387 14 5946 1484 15 5977 117 16 6139 664 Update: Here is the output of count(data$category) : x freq 1 3289 198142 2 3401 97864 3 3482 38172 4 3502 59386 5 3601 391800 6 3783 201409 7 4029 111075 8 4166 6749 9 4178 239978 10 4785 6473 11 5108 32083 12 5245 590060 13 5637 98785 14 5946 401625 15 5977 28769 16 6139 183258 But when I try setting the probability I get the following error : > catCount <- length(unique(data$category)) > probabilities <- rep(c(1/catCount),catCount) > train_set <- data[sample(nrow(data),10000,prob=probabilities),] Error in sample.int(x, size, replace, prob) : incorrect number of probabilities I understand that the sample function is randomly picking between the row number but I can't figure out how to associate that with the probability over the categories. Question : How can I sample my data over an equal probability for the category variable? Thanks in advance.
I guess you could do this with some simple base R operation, though you should remember that you are using probabilities here within sample, thus getting the exact amount per each combination won't work using this method, though you can get close enough for large enough sample. Here's an example data set.seed(123) data <- data.frame(category = sample(rep(letters[1:10], seq(1000, 10000, by = 1000)), 55000)) Then probs <- 1/prop.table(table(data$category)) # Calculating relative probabilities data$probs <- probs[match(data$category, names(probs))] # Matching them to the correct rows set.seed(123) train_set <- data[sample(nrow(data), 1000, prob = data$probs), ] # Sampling table(train_set$category) # Checking frequencies # a b c d e f g h i j # 94 103 96 107 105 99 100 96 107 93 Edit: So here's a possible data.table equivalent library(data.table) setDT(data)[, probs := .N, category][, probs := .N/probs] train_set <- data[sample(.N, 1000, prob = probs)] Edit #2: Here's a very nice solution using the dplyr package contributed by #Khashaa and #docendodiscimus The nice thing about this solution is that it returns the exact sample size within each group library(dplyr) train_set <- data %>% group_by(category) %>% sample_n(1000) Edit #3: It seems that data.table equivalent to dplyr::sample_n would be library(data.table) train_set <- setDT(data)[data[, sample(.I, 1000), category]$V1] Which will also return the exact sample size within each group
Find the non zero values and frequency of those values in R
I have a data which has two parameters, they are data/time and flow. The flow data is intermittent flow. Lets say at times there is zero flow and suddenly the flow starts and there will be non-zero values for sometime and then the flow will be zero again. I want to understand when the non-zero values occur and how long does each non-zero flow last. I have attached the sample dataset at this location https://www.dropbox.com/s/ef1411dq4gyg0cm/sampledataflow.csv The data is 1 minute data. I was able to import the data into R as follows: flow <- read.csv("sampledataflow.csv") summary(flow) names(flow) <- c("Date","discharge") flow$Date <- strptime(flow$Date, format="%m/%d/%Y %H:%M") sapply(flow,class) plot(flow$Date, flow$discharge,type="l") I made plot to see the distribution but couldn't get a clue where to start to get the frequency of each non zero values. I would like to see a output table as follows: Date Duration in Minutes Please let me know if I am not clear here. Thanks. Additional Info: I think we need to check the non-zero value first and then find how many non zero values are there continuously before it reaches zero value again. What I want to understand is the flow release durations. For eg. in one day there might be multiple releases and I want to note at what time did the release start and how long did it continue before coming to value zero. I hope this explain the problem little better.
The first point is that you have too many NA in your data. In case you want to look into it. If I understand correctly, you require the count of continuous 0's followed by continuous non-zeros, zeros, non-zeros etc.. for each date. This can be achieved with rle of course, as also mentioned by #mnel under comments. But there are quite a few catches. First, I'll set up the data with non-NA entries: flow <- read.csv("~/Downloads/sampledataflow.csv") names(flow) <- c("Date","discharge") flow <- flow[1:33119, ] # remove NA entries # format Date to POSIXct to play nice with data.table flow$Date <- as.POSIXct(flow$Date, format="%m/%d/%Y %H:%M") Next, I'll create a Date column: flow$g1 <- as.Date(flow$Date) Finally, I prefer using data.table. So here's a solution using it. # load package, get data as data.table and set key require(data.table) flow.dt <- data.table(flow) # set key to both "Date" and "g1" (even though, just we'll use just g1) # to make sure that the order of rows are not changed (during sort) setkey(flow.dt, "Date", "g1") # group by g1 and set data to TRUE/FALSE by equating to 0 and get rle lengths out <- flow.dt[, list(duration = rle(discharge == 0)$lengths, val = rle(discharge == 0)$values + 1), by=g1][val == 2, val := 0] > out # just to show a few first and last entries # g1 duration val # 1: 2010-05-31 120 0 # 2: 2010-06-01 722 0 # 3: 2010-06-01 138 1 # 4: 2010-06-01 32 0 # 5: 2010-06-01 79 1 # --- # 98: 2010-06-22 291 1 # 99: 2010-06-22 423 0 # 100: 2010-06-23 664 0 # 101: 2010-06-23 278 1 # 102: 2010-06-23 379 0 So, for example, for 2010-06-01, there are 722 0's followed by 138 non-zeros, followed by 32 0's followed by 79 non-zeros and so on...
I looked a a small sample of the first two days > do.call( cbind, tapply(flow$discharge, as.Date(flow$Date), function(x) table(x > 0) ) ) 2010-06-01 2010-06-02 FALSE 1223 911 TRUE 217 529 # these are the cumulative daily durations of positive flow. You may want this transposed in which case the t() function should succeed. Or you could use rbind. If you jsut wante the number of flow-postive minutes, this would also work: tapply(flow$discharge, as.Date(flow$Date), function(x) sum(x > 0, na.rm=TRUE) ) #-------- 2010-06-01 2010-06-02 2010-06-03 2010-06-04 2010-06-05 2010-06-06 2010-06-07 2010-06-08 217 529 417 463 0 0 263 220 2010-06-09 2010-06-10 2010-06-11 2010-06-12 2010-06-13 2010-06-14 2010-06-15 2010-06-16 244 219 287 234 31 245 311 324 2010-06-17 2010-06-18 2010-06-19 2010-06-20 2010-06-21 2010-06-22 2010-06-23 2010-06-24 299 305 124 129 295 296 278 0 To get the lengths of intervals with discharge values greater than zero: tapply(flow$discharge, as.Date(flow$Date), function(x) rle(x>0)$lengths[rle(x>0)$values] ) #-------- $`2010-06-01` [1] 138 79 $`2010-06-02` [1] 95 195 239 $`2010-06-03` [1] 57 360 $`2010-06-04` [1] 6 457 $`2010-06-05` integer(0) $`2010-06-06` integer(0) ... Snipped output If you want to look at the distribution of these durations you will need to unlist that result. (And remember that the durations which were split at midnight may have influenced the counts and durations.) If you just wanted durations without dates, then use this: flowrle <- rle(flow$discharge>0) flowrle$lengths[!is.na(flowrle$values) & flowrle$values] #---------- [1] 138 79 95 195 296 360 6 457 263 17 203 79 80 85 30 189 17 270 127 107 31 1 [23] 2 1 241 311 229 13 82 299 305 3 121 129 295 3 2 291 278