【问题标题】:How to group a data frame by variables that are approximately the same?如何按大致相同的变量对数据框进行分组?
【发布时间】:2021-06-17 04:22:47
【问题描述】:

我有以下数据框,其中包含每个县的数据:

    A tibble: 6 x 4
# Groups:   countyfips, day_after_reopening, deciles_income [6]
  countyfips day_after_reopening deciles_income winner2016  
       <int> <drtn>                       <int> <chr>       
1       1001 -109 days                        8 Donald Trump
2       1001 -102 days                        8 Donald Trump
3       1001  -95 days                        8 Donald Trump
4       1001  -88 days                        8 Donald Trump
5       1001  -81 days                        8 Donald Trump
6       1001  -74 days                        8 Donald Trump

我想按 day_after_reopening 列对其进行分组。然而,问题是每个县的 day_after_reopening 数字略有不同,因为每个县的观测结果都是同时进行的,但是每个县都在一周中的不同日子开放(例如,在两个县之外,我会喜欢在同一组中,一个可能有-109,另一个可能有-108)。

您如何将这两个数值非常相似的县组合在一起?谢谢。

【问题讨论】:

  • 首先,分组的意义何在?之后你打算做什么?好主意可能是创建一个新变量,将不同的时间范围合并在一起 - 0-25、26-50 等......或者按四分位数......您可以使用 case_when 来制作该变量。然后您可以按该新变量进行分组。
  • 您是否有特定的阈值,或者您希望它是数据驱动的?如果是数据驱动的,可以试试cut()
  • 我不太确定您所说的数据驱动是什么意思,但我想根据从 0 开始的 7 天间隔对其进行拆分。@Mike
  • 我想从 0 开始以 7 天为间隔进行拆分。@gregdubrow
  • 所以你想要一个变量,我们称之为day_after_group,即0-6、7-13、14-20等?我认为 cut() 函数可以做到这一点。可能有助于将伪造的示例数据框作为代表,因此您可以显示所需结果的 sn-p。

标签: r dataframe dplyr


【解决方案1】:

您可以根据数字之间的一些预定义差异创建人工组。

我在下面创建了一个示例:

require(dplyr)

# Difference max that you want
difference_max <- 2

# Create dummy data frame
day_after_reopening <- c(108, 109, 107, 50, 51, 68, 69, 67, 108, 109, 55, 56, 57, 100, 101, 101, 100,56)
df <- data.frame(day_after_reopening = day_after_reopening, index = seq(1:length(day_after_reopening)))

# Order the interesting column
df <- df[order(df$day_after_reopening),]
df$test <- c(diff(df$day_after_reopening, lag = 1), 0)

# Create the breaks where the difference value is greater than a selected value
breaks <- df[df$test > difference_max,]
breaks$test <- "here"
df <- rbind(breaks, df)
df <- df[order(df$day_after_reopening, df$test),]

# Create the split points and grouping
 df <- df %>% 
        mutate(split_point = test < "here",
              breaks = with(rle(split_point), rep(seq_along(lengths), lengths))) %>%
        filter(split_point) %>% 
        group_by(breaks) %>%
        summarise(day_after_reopening_mean = mean(day_after_reopening))
 
    > df
# A tibble: 5 x 2
  breaks day_after_reopening_mean
   <int>                    <dbl>
1      1                     50.5
2      3                     56  
3      5                     68  
4      7                    100. 
5      9                    108. 

【讨论】:

  • 如果日期之间的差异较大,这可能会起作用,但我担心变量之间只会有一天的差异(因为它是一个包含数百个美国县的大型数据集)。
  • 但是您可以打破任何差异之间的数据:2、1 或 0.1 天。但我不知道这种微小的差异是否可以识别群体。这是我所理解的。
  • 知道我理解你的恐惧,如果大数据框架中的差异只有 1 天,你可以只识别一组。这是有道理的。
【解决方案2】:

好的,听起来您首先要获得最大天数,以便知道要走多远,新数据帧的代码可能如下所示(我从未使用过 cut() 所以不知道如何更自动地做到这一点):

df2 <- df1 %>%
mutate(day_after_grp = 
   case_when(day_after_reopening >=0 & day_after_reopening <=6 ~ "0-6",
        day_after_reopening >=7 & day_after_reopening <=13 ~ "7-13",
        day_after_reopening >=14 & day_after_reopening <=20 ~ "14-20",

等到最大值。然后,您将有一个新变量“day_after_grp”用于分组。

同样,可能有一种更加程序化的方式来完成它/更少的复制/粘贴。

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2022-01-22
    • 2021-11-28
    • 2021-09-26
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    相关资源
    最近更新 更多