Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于常用日期创建组变量_R_Date_Grouping - Fatal编程技术网

R 基于常用日期创建组变量

R 基于常用日期创建组变量,r,date,grouping,R,Date,Grouping,我有一个包含动物ID和日期的大型数据集。这个数据集中有两个组,但没有分组变量,因此我必须根据它们的共同日期推断谁属于哪个组 虚拟数据 mydf我的想法是,您只需将每个日期分配给一个组,然后取每个ID的组平均值。然后可以从那里四舍五入到最接近的整数。在这种情况下,ID==5的group平均值为1.33 library(dplyr) mydf %>% mutate(group = case_when( Date %in% as.Date(c("2012-01-01", "2012

我有一个包含动物ID和日期的大型数据集。这个数据集中有两个组,但没有分组变量,因此我必须根据它们的共同日期推断谁属于哪个组

虚拟数据


mydf我的想法是,您只需将每个日期分配给一个组,然后取每个
ID
的组平均值。然后可以从那里四舍五入到最接近的整数。在这种情况下,
ID==5的
group
平均值为
1.33

library(dplyr)
mydf %>% 
  mutate(group = case_when(
    Date %in% as.Date(c("2012-01-01", "2012-01-03")) ~ 1,
    Date %in% as.Date(c("2012-01-02", "2012-01-04")) ~ 2,
    TRUE                                    ~ NA_real_
  )) %>% 
  group_by(ID) %>% 
  summarise(likely_group = mean(group) %>% round)
这将为您提供以下信息:

# A tibble: 10 x 2
      ID likely_group
   <dbl>        <dbl>
 1     1            1
 2     2            1
 3     3            1
 4     4            1
 5     5            1
 6     6            2
 7     7            2
 8     8            2
 9     9            2
10    10            2
#一个tible:10x2
我很喜欢你的团队
1     1            1
2     2            1
3     3            1
4     4            1
5     5            1
6     6            2
7     7            2
8     8            2
9     9            2
10    10            2

只要一个
ID
的组之间没有均匀的分割,这种方法就可以工作。但是目前还没有一种方法来解决这个问题,提供的信息。

< P>作为一个通用的解决方案,您可以考虑使用k-均值作为一种自动方式,将数据按照与其他IDS的相似性分割成组。 首先,我将数据转换为宽格式,以便每个ID都有一行。然后将其输入基本
kmeans
函数,以获取集群输出作为列表,然后
purr::pull
仅提取该列表的赋值部分

library(tidyverse)
mydf_wide <- mydf %>% 
  mutate(x = 1) %>%
  spread(Date, x, fill = 0)

mydf_wide
 #   ID 2012-01-01 2012-01-02 2012-01-03 2012-01-04
 #1   1          1          0          1          0
 #2   2          1          0          1          0
 #3   3          1          0          1          0
 #4   4          1          0          1          0
 #5   5          1          1          1          0
 #6   6          0          1          0          1
 #7   7          0          1          0          1
 #8   8          0          1          0          1
 #9   9          0          1          0          1
 #10 10          0          0          0          1

clusters <- mydf_wide %>% 
  kmeans(centers = 2) %>%
  pluck("cluster")

clusters
 # [1] 2 2 2 2 2 1 1 1 1 1

ID出现的最早日期是否表示其组?不幸的是,否。我确实尝试过这种方法,但失败了,因为ID可能在与另一个组关联的日期首次出现,但所有后续出现都表明它实际上属于另一个组。此解决方案非常完美。非常感谢你的帮助!这适用于我的虚拟数据集,但我不确定是否可以将其应用于我的真实数据集,因为我有大量的日期,并且无法知道哪个组出现在哪个日期。谢谢你!
# A tibble: 10 x 2
      ID likely_group
   <dbl>        <dbl>
 1     1            1
 2     2            1
 3     3            1
 4     4            1
 5     5            1
 6     6            2
 7     7            2
 8     8            2
 9     9            2
10    10            2
library(tidyverse)
mydf_wide <- mydf %>% 
  mutate(x = 1) %>%
  spread(Date, x, fill = 0)

mydf_wide
 #   ID 2012-01-01 2012-01-02 2012-01-03 2012-01-04
 #1   1          1          0          1          0
 #2   2          1          0          1          0
 #3   3          1          0          1          0
 #4   4          1          0          1          0
 #5   5          1          1          1          0
 #6   6          0          1          0          1
 #7   7          0          1          0          1
 #8   8          0          1          0          1
 #9   9          0          1          0          1
 #10 10          0          0          0          1

clusters <- mydf_wide %>% 
  kmeans(centers = 2) %>%
  pluck("cluster")

clusters
 # [1] 2 2 2 2 2 1 1 1 1 1
mydf_wide %>%
  mutate(cluster = clusters) %>%

  # ggplot works better with long (tidy) data...
  gather(date, val, -ID, -cluster) %>%
  filter(val != 0) %>%
  arrange(cluster) %>%

  ggplot(aes(date, ID, color = as.factor(cluster))) + 
  geom_point(size = 5) +
  scale_y_continuous(breaks = 1:10, minor_breaks = NULL) +
  scale_color_discrete(name = "cluster")