R 如何在数据框列中首次出现多个值之间查找最小和最大中间项目数
我有一个因子向量,叫做47级的类别R 如何在数据框列中首次出现多个值之间查找最小和最大中间项目数,r,dataframe,R,Dataframe,我有一个因子向量,叫做47级的类别 Categories = as.factor(sort(make.unique(rep(letters, length.out = 47), sep=''))) [1] a a1 b b1 c c1 d d1 e e1 f f1 g g1 h h1 i i1 j j1 k k1 l l1 m m1 n n1 o o1 p p1 q q1 r r1 s s1 t [40] t1 u u1 v w x y z
Categories = as.factor(sort(make.unique(rep(letters, length.out = 47), sep='')))
[1] a a1 b b1 c c1 d d1 e e1 f f1 g g1 h h1 i i1 j j1 k k1 l l1 m m1 n n1 o o1 p p1 q q1 r r1 s s1 t
[40] t1 u u1 v w x y z
47 Levels: a a1 b b1 c c1 d d1 e e1 f f1 g g1 h h1 i i1 j j1 k k1 l l1 m m1 n n1 o o1 p p1 q q1 r r1 s s1 t t1 u u1 ... z
我有另一个向量叫做cat,它有9个级别
cat = Categories[c(7,42,43,24,45,26,35,6,15)]
[1] d u1 v l1 x m1 r c1 h
47 Levels: a a1 b b1 c c1 d d1 e e1 f f1 g g1 h h1 i i1 j j1 k k1 l l1 m m1 n n1 o o1 p p1 q q1 r r1 s s1 t t1 u u1 ... z
我还有一个包含36行的数据框My_数据。数据框中的一列多次出现cat中的值
My_Data = data.frame(name = make.unique(rep(c(1:10,LETTERS), length.out = 36), sep=''), cat = sample(rep(cat,4),36,replace = FALSE), position = 0)
name cat position
1 1 v 0
2 2 r 0
3 3 h 0
4 4 m1 0
5 5 h 0
6 6 u1 0
7 7 l1 0
8 8 h 0
9 9 u1 0
10 10 x 0
11 A x 0
12 B v 0
13 C d 0
14 D c1 0
15 E r 0
16 F v 0
17 G l1 0
18 H d 0
19 I l1 0
20 J c1 0
21 K u1 0
22 L x 0
23 M v 0
24 N d 0
25 O l1 0
26 P m1 0
27 Q r 0
28 R m1 0
29 S h 0
30 T m1 0
31 U c1 0
32 V d 0
33 W r 0
34 X x 0
35 Y c1 0
36 Z u1 0
使用下面的代码,我可以填充上面给出的位置列,以反映cat列中出现值的次数:
transform(My_Data, position = ave(as.character(cat), cat, FUN = seq_along))
dataframe My_数据的前15行如下所示:
name cat position
1 1 v 1
2 2 r 1
3 3 h 1
4 4 m1 1
5 5 h 2
6 6 u1 1
7 7 l1 1
8 8 h 3
9 9 u1 2
10 10 x 1
11 A x 2
12 B v 2
13 C d 1
14 D c1 1
15 E r 2
现在,我想计算cat列中任意两个连续出现的相同值之间的最小和最大中间项目数
我该怎么做?如果我理解你的问题,这里有一个选项:
library(tidyverse)
# Data
Categories = as.factor(sort(make.unique(rep(letters, length.out = 47), sep='')))
cat = Categories[c(7,42,43,24,45,26,35,6,15)]
# Set a seed for reproducibility
set.seed(5)
My_Data = data.frame(name = make.unique(rep(c(1:10,LETTERS), length.out = 36), sep=''),
cat = sample(rep(cat,4),36,replace = FALSE),
position = 0)
下面的代码总结了cat
的每一级的最小和最大干预行数
# Summarise to give min and max number rows between each occurrence
My_Data %>%
mutate(row=1:n()) %>%
group_by(cat) %>%
summarise(min.diff=min(diff(row)-1, na.rm=TRUE),
max.diff=max(diff(row)-1, na.rm=TRUE))
# Add column with intervening number of rows between each occurrence in cat
My_Data %>%
mutate(row=1:n()) %>%
group_by(cat) %>%
mutate(diff=c(NA,diff(row)-1)) %>%
select(-row)
名称cat位置差异
1 c1 0 NA
2 m1 0 NA
3x0NA
4d0na
5 l1 0 NA
6 l10 0
7 r 0 NA
8 8 c1 06
9h0na
10伏0毫安
如果我理解你的问题,这里有一个选择:
library(tidyverse)
# Data
Categories = as.factor(sort(make.unique(rep(letters, length.out = 47), sep='')))
cat = Categories[c(7,42,43,24,45,26,35,6,15)]
# Set a seed for reproducibility
set.seed(5)
My_Data = data.frame(name = make.unique(rep(c(1:10,LETTERS), length.out = 36), sep=''),
cat = sample(rep(cat,4),36,replace = FALSE),
position = 0)
下面的代码总结了cat
的每一级的最小和最大干预行数
# Summarise to give min and max number rows between each occurrence
My_Data %>%
mutate(row=1:n()) %>%
group_by(cat) %>%
summarise(min.diff=min(diff(row)-1, na.rm=TRUE),
max.diff=max(diff(row)-1, na.rm=TRUE))
# Add column with intervening number of rows between each occurrence in cat
My_Data %>%
mutate(row=1:n()) %>%
group_by(cat) %>%
mutate(diff=c(NA,diff(row)-1)) %>%
select(-row)
名称cat位置差异
1 c1 0 NA
2 m1 0 NA
3x0NA
4d0na
5 l1 0 NA
6 l10 0
7 r 0 NA
8 8 c1 06
9h0na
10伏0毫安
下面是一个使用
lag()
的简洁解决方案:
库(tidyverse)
#创建数据帧
种子(1)
类别%
突变(内部=行-滞后(行)-1)%>%
总结(min_inter=min(inter,na.rm=T),max_inter=max(inter,na.rm=T))
结果:
# A tibble: 9 x 3
cat min_inter max_inter
<fctr> <dbl> <dbl>
1 c1 0 10
2 d 4 11
3 h 0 8
4 l1 0 6
5 m1 1 3
6 r 0 16
7 u1 2 5
8 v 1 23
9 x 6 15
#一个tible:9 x 3
cat最小值与最大值之间
1 c1 0 10
2 d 4 11
3h08
4 l1 06
5 m1 13
6 r 0 16
7 u1 2 5
8伏123
9 x 6 15
这里有一个使用lag()的简洁解决方案。
:
库(tidyverse)
#创建数据帧
种子(1)
类别%
突变(内部=行-滞后(行)-1)%>%
总结(min_inter=min(inter,na.rm=T),max_inter=max(inter,na.rm=T))
结果:
# A tibble: 9 x 3
cat min_inter max_inter
<fctr> <dbl> <dbl>
1 c1 0 10
2 d 4 11
3 h 0 8
4 l1 0 6
5 m1 1 3
6 r 0 16
7 u1 2 5
8 v 1 23
9 x 6 15
#一个tible:9 x 3
cat最小值与最大值之间
1 c1 0 10
2 d 4 11
3h08
4 l1 06
5 m1 13
6 r 0 16
7 u1 2 5
8伏123
9 x 6 15