I want to split data in R by varying block sizes but each observation is unique -
i have managed read in data file, , subset out 2 columns of info want work with. stuck because need split data chunks of varying sizes , apply function (mean, sd) them, save chunks , plot sd each. otherwise known block averaging. right have data frame 2 columns , 10005 rows. head of looks this:
frame ca 1 0.773
is there efficient way subset pieces of data a:b can dictate how data broken "frame" column? have found answers on here not sure mean or if work.
chunk <- function(x, n) (mapply(function(a, b) (x[a:b]), seq.int(from=1, to=length(x), by=n), pmin(seq.int(from=1, to=length(x), by=n)+(n-1), length(x)), simplify=false))
i'm not sure if you're looking closure, data frame can subsetted arbitrary indices.
(if frame
can subsetted a:b
, sequence , subset may made row index?)
df <- data.frame(group = sample(c("a", "b"), 20, replace = t), val = rnorm(20)) # closure - returns function accepts , subsetter <- function(from, to) { function(x) { x[from:to, ] } } # , specified sub1 <- subsetter(2, 4) sub2 <- subsetter(1, 5) # data split to sub1(df) #group val #2 0.5518802 #3 b 1.5955093 #4 -0.8132578 sub2(df) # group val #1 b 0.4780080 #2 0.5518802 #3 b 1.5955093 #4 -0.8132578 #5 b 0.4449554
Comments
Post a Comment