H everyone, I have a little problem, and I hope you can help me.
I have a dataset:
value <- c(3,4,5,4,3,4,5,4,3,4,3,4,6,5,4,5,6,7,5,4,6,4,6,3,2,6,7,4,7,8,5,6,7,5,5,7,8,7,45,3,4,3,4,6,4,3,2,1,2,3,4) Sub <- c(rep("sub1", 10), rep("sub2",25), rep("sub3", 16)) dat <-as.data.frame(Sub, value)
It'a only an example, but it is useful to give you an idea of my purposes.
What I need to do is:
- to divide for each subject the variable value in 10 part and to calculate a statistics, such as the mean
- I should create a dataframe like this:
p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 Sub1 Sub2 Sub...
I used a function like this to calculate a percentage of rows to analyze.
subset_percent <- function(x, start=0, end=100) {
stopifnot(
is.numeric(start), is.numeric(end),
start < end, start >= 0, end <= 100
)
nr <- nrow(x)
rows <- 1:nr
srt <- ceiling(start*nr/100)
end <-floor(end*nr/100)
x[srt:end,]
}
My intent was to use dplyr to apply this fu> nction to each group:
newdat <- dat %>% group_by(Sub) %>% summarise( p10 = mean(subset_percent(dat,0,10)$value), p20 = mean(subset_percent(dat,10,20)$value), p30 = mean(subset_percent(dat,20,30)$value), p40 = mean(subset_percent(dat,30,40)$value), p50 = mean(subset_percent(dat,40,50)$value), p60 = mean(subset_percent(dat,50,60)$value), p70 = mean(subset_percent(dat,60,70)$value), p80 = mean(subset_percent(dat,70,80)$value), p90 = mean(subset_percent(dat,80,90)$value), p100 = mean(subset_percent(dat,90,100)$value))
However the output was not correct, because I got this erroneous result:
Sub p10 p20 p30 p40 p50 p60 p70 p80 p90 p100 <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 sub1 3.8 3.9 4.07 4.4 4.36 4.7 4.83 5.98 5.78 5.39 2 sub2 3.8 3.9 4.07 4.4 4.36 4.7 4.83 5.98 5.78 5.39 3 sub3 3.8 3.9 4.07 4.4 4.36 4.7 4.83 5.98 5.78 5.39
Do you have any advice?
Thank you