在 R 中，您可以按长度对事件之间的间隔进行分组吗？

``````structure(list(Interval.1 = 0.1545, Interval.2 = 0.1605, Interval.3 = 0.1795,
Interval.4 = 3.7845, Interval.5 = 0.14, Interval.6 = 0.1735,
Interval.7 = 123.559, Interval.8 = 0.6245, Interval.9 = 0.1685,
Interval.10 = 0.1535, Interval.11 = 0.1935, Interval.12 = 1.408,
Interval.13 = 0.159, Interval.14 = 0.2375, Interval.15 = 0.162,
Interval.16 = 0.172, Interval.17 = 4.4575, Interval.18 = 0.1865,
Interval.19 = 0.1545, Interval.20 = 0.1795, Interval.21 = 0.151,
Interval.22 = -219.9355), class = "data.frame", row.names = "a25")
``````

``````=(SUMIF(B2:BI2,">0",B2:BI2)-SUMIF(B2:BI2,">1",B2:BI2))/(COUNTIF(B2:BI2,">0")-COUNTIF(B2:BI2,">1"))
``````

r 中的解决方案是首选，但是当找到 r 解决方案时，可以做同样事情的 Excel 函数会很有帮助。谢谢！

``````library(dplyr)
library(tidyr)

dat<-structure(list(Interval.1 = 0.1545, Interval.2 = 0.1605, Interval.3 = 0.1795,
Interval.4 = 3.7845, Interval.5 = 0.14, Interval.6 = 0.1735,
Interval.7 = 123.559, Interval.8 = 0.6245, Interval.9 = 0.1685,
Interval.10 = 0.1535, Interval.11 = 0.1935, Interval.12 = 1.408,
Interval.13 = 0.159, Interval.14 = 0.2375, Interval.15 = 0.162,
Interval.16 = 0.172, Interval.17 = 4.4575, Interval.18 = 0.1865,
Interval.19 = 0.1545, Interval.20 = 0.1795, Interval.21 = 0.151,
Interval.22 = -219.9355), class = "data.frame", row.names = "a25")%>%
gather() #Gather to make the data long, rather than wide

dat["NumberToAverage"] <- rnorm(1:nrow(dat)) #Adding a number to average when grouped

dat2<-dat%>%
mutate("Group" = cumsum( value > 1)) #Creating a group, any time the cumulative sum is over 1, it adds a new group

dat2
``````

``````           key     value NumberToAverage Group
1   Interval.1    0.1545      0.35351809     0
2   Interval.2    0.1605      0.62707697     0
3   Interval.3    0.1795     -1.55307710     0
4   Interval.4    3.7845      0.16435082     1
5   Interval.5    0.1400     -0.48406590     1
6   Interval.6    0.1735      0.55728037     1
7   Interval.7  123.5590      2.15224668     2
8   Interval.8    0.6245     -1.68274497     2
9   Interval.9    0.1685     -0.02153693     2
10 Interval.10    0.1535     -0.02521306     2
11 Interval.11    0.1935     -1.85274486     2
12 Interval.12    1.4080      0.88130382     3
13 Interval.13    0.1590     -1.26127262     3
14 Interval.14    0.2375     -0.27524457     3
15 Interval.15    0.1620     -1.52575130     3
16 Interval.16    0.1720      0.02155710     3
17 Interval.17    4.4575      0.78966219     4
18 Interval.18    0.1865      1.10879272     4
19 Interval.19    0.1545     -0.83787817     4
20 Interval.20    0.1795     -0.43154448     4
21 Interval.21    0.1510     -0.18844497     4
22 Interval.22 -219.9355     -1.06143151     4
``````

``````dat2%>%
group_by(Group)%>% #Group by the groups
summarise(Mean = mean(NumberToAverage), #Mean of the NumberToAverage by Group
SD = sd(NumberToAverage))%>% #Standard deviation of the NumberToAverage by Group
ungroup() #From my understanding it's good practice to always ungroup in case further modification of the dataframe is needed

Group    Mean    SD
<int>   <dbl> <dbl>
1     0 -0.191  1.19
2     1  0.0792 0.526
3     2 -0.286  1.62
4     3 -0.432  0.980
5     4 -0.103  0.876
``````

0 条评论