根据列值按组对行进行聚类

时间:2018-06-21 08:56:16

标签: r dplyr seq

我有以下内容:

df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2),
             Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1))

我想要这个:

df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2),
             Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1),
             Cluster = c(0,1,1,1,2,2,2,3,3,3,0,0,1))

如何使用dplyr获取“簇”列,其中必须对数字1进行排序,直到出现第一个0为止?

连续的0必须保持该值,直到出现新的1。

编辑

如果有很多列,我该怎么做?

假设我有99个obs列,并且我想创建99个群集,每列一个。像这样:

df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2),
Obs1 = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1),
Obs2 = c(0,0, 0, 1, 1,1,0, 1, 0, 1, 0,0,1),
ClusterObs1 = c(0,1,1,1,2,2,2,3,3,3,0,0,1),
ClusterObs2 = c(0,0,0,1,1,1,1,2,2,3,0,0,1))

2 个答案:

答案 0 :(得分:7)

以下是使用rle的选项:

df %>% 
  group_by(ID) %>% 
  mutate(clust = with(rle(Obs), rep(cumsum(values == 1), lengths)))
# # A tibble: 13 x 4
# # Groups:   ID [2]
# ID   Obs Cluster clust
# <dbl> <dbl>   <dbl> <int>
# 1    1.    0.      0.     0
# 2    1.    1.      1.     1
# 3    1.    1.      1.     1
# 4    1.    0.      1.     1
# 5    1.    1.      2.     2
# 6    1.    0.      2.     2
# 7    1.    0.      2.     2
# 8    1.    1.      3.     3
# 9    1.    1.      3.     3
# 10    1.    1.      3.     3
# 11    2.    0.      0.     0
# 12    2.    0.      0.     0
# 13    2.    1.      1.     1

这是它的主要部分:

rle(df$Obs)
#Run Length Encoding
#  lengths: int [1:8] 1 2 1 1 2 3 2 1
#  values : num [1:8] 0 1 0 1 0 1 0 1

这告诉您Obs列中每个1s或0s的长度是多长时间(我暂时忽略了ID分组)。

我们现在需要的是累计计算1的条纹次数,并简单地累加值1的累加。

with(rle(df$Obs), cumsum(values == 1))
#[1] 0 1 1 2 2 3 3 4

到目前为止,到目前为止,我们需要将这些值重复较长的次数,因此,我们使用来自replengths的rle信息:

with(rle(df$Obs), rep(cumsum(values == 1), lengths))
# [1] 0 1 1 1 2 2 2 3 3 3 3 3 4

最后,我们按ID组进行操作。


如果您需要为不同的obs-columns创建多个cluster-columns,则可以轻松地按以下步骤进行操作:

df %>% 
  group_by(ID) %>% 
  mutate_at(vars(starts_with("Obs")), 
            funs(cluster= with(rle(.), rep(cumsum(values == 1), lengths))))

# # A tibble: 13 x 7
# # Groups:   ID [2]
# ID  Obs1  Obs2 ClusterObs1 ClusterObs2 Obs1_cluster Obs2_cluster
# <dbl> <dbl> <dbl>       <dbl>       <dbl>        <int>        <int>
# 1    1.    0.    0.          0.          0.            0            0
# 2    1.    1.    0.          1.          0.            1            0
# 3    1.    1.    0.          1.          0.            1            0
# 4    1.    0.    1.          1.          1.            1            1
# 5    1.    1.    1.          2.          1.            2            1
# 6    1.    0.    1.          2.          1.            2            1
# 7    1.    0.    0.          2.          1.            2            1
# 8    1.    1.    1.          3.          2.            3            2
# 9    1.    1.    0.          3.          2.            3            2
# 10    1.    1.    1.          3.          3.            3            3
# 11    2.    0.    0.          0.          0.            0            0
# 12    2.    0.    0.          0.          0.            0            0
# 13    2.    1.    1.          1.          1.            1            1

其中df是:

df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2), Obs1 = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1), Obs2 = c(0,0, 0, 1, 1,1,0, 1, 0, 1, 0,0,1), ClusterObs1 = c(0,1,1,1,2,2,2,3,3,3,0,0,1), ClusterObs2 = c(0,0,0,1,1,1,1,2,2,3,0,0,1))

答案 1 :(得分:2)

这是一个很有趣的问题,因此这里有一个data.table解决方案:

# Packages used
library(data.table)
library(magrittr)

# Setup
setDT(df)
df[, Obs := as.integer(Obs)]

# Calculations
df[, Cluster := cumsum(!Obs), by = ID] %>%
  .[, Cluster := Cluster - rowid(Obs) * !Obs, by = rleid(Obs)] %>%
  .[, Cluster := frank(Cluster, ties.method = "dense") - 1L, by = ID]

df
    ID Obs Cluster
 1:  1   0       0
 2:  1   1       1
 3:  1   1       1
 4:  1   0       1
 5:  1   1       2
 6:  1   0       2
 7:  1   0       2
 8:  1   1       3
 9:  1   1       3
10:  1   1       3
11:  2   0       0
12:  2   0       0
13:  2   1       1