【问题标题】:Performing in group operations in R在 R 中执行分组操作
【发布时间】:2018-06-27 14:14:32
【问题描述】:

我有一个数据,其中我在一个表中有 2 个字段 sf -> Customer id 和 Buy_dateBuy_date 是唯一的,但对于每个客户,但对于每个客户,Buy_dates 可以有超过 3 个不同的值。我想计算每个Customer 的连续Buy_date 的差异及其平均值。我该怎么做。

例子

Customer   Buy_date
1          2018/03/01
1          2018/03/19
1          2018/04/3
1          2018/05/10
2          2018/01/02
2          2018/02/10
2          2018/04/13

我想要格式为每个客户的结果

Customer  mean

【问题讨论】:

  • 请发布一些示例代码向我们展示您所做的事情。并请放一些数据
  • 嗨。欢迎来到 SO。请阅读stackoverflow.com/questions/5963269/… 并使用dput() 添加您的数据框示例。
  • 请在代码中包含一个小数据示例,而不是在散文中描述,例如对最小数据集使用dputdput(head(data)),如this guide (只需ctrl-F 用于'dput')。
  • library(dplyr); df %>% group_by(Customer) %>% mutate(mean = mean(Buy_date)) ?
  • @symbolrush,这可能应该是 mean=mean(diff(Buy_date)),因为 OP 声明 “连续 Buy_date 的差异” (尽管我不确定“连续”如何适合。 ..).

标签: r


【解决方案1】:

这是dplyr 解决方案。

您的数据:

df <- data.frame(Customer = c(1,1,1,1,2,2,2), Buy_date = c("2018/03/01", "2018/03/19", "2018/04/3", "2018/05/10", "2018/01/02", "2018/02/10", "2018/04/13"))

分组,意思是Buy_date计算总结:

library(dplyr)
df %>% group_by(Customer) %>% mutate(mean = mean(as.POSIXct(Buy_date))) %>% group_by(Customer, mean) %>% summarise()

输出:

# A tibble: 2 x 2
# Groups:   Customer [?]
  Customer mean               
     <dbl> <dttm>             
1        1 2018-03-31 06:30:00
2        2 2018-02-17 15:40:00

或者正如@r2evans 在他对Buy_dates 之间的连续几天的评论中指出的那样:

df %>% group_by(Customer) %>% mutate(mean = mean(diff(as.POSIXct(Buy_date)))) %>% group_by(Customer, mean) %>% summarise()

输出:

# A tibble: 2 x 2
# Groups:   Customer [?]
  Customer mean            
     <dbl> <time>          
1        1 23.3194444444444
2        2 50.4791666666667

【讨论】:

  • 我尝试使用它,但它给出了一个错误:quickdf(.data[names(cols)]) 中的错误:length(rows) == 1 is not TRUE
【解决方案2】:

我不确定所需的输出,但这是我认为您想要的。

library(dplyr)
library(zoo)
dat <- read.table(text = 
"Customer   Buy_date
1          2018/03/01
1          2018/03/19
1          2018/04/3
1          2018/05/10
2          2018/01/02
2          2018/02/10
2          2018/04/13", header = T, stringsAsFactors = F)


dat$Buy_date <- as.Date(dat$Buy_date)

dat %>% group_by(Customer) %>% mutate(diff_between = as.vector(diff(zoo(Buy_date), na.pad=TRUE)), 
                                      mean_days = mean(diff_between, na.rm = TRUE))

这会产生:

    Customer Buy_date   diff_between mean_days
     <int> <date>            <dbl>     <dbl>
1        1 2018-03-01           NA      23.3
2        1 2018-03-19           18      23.3
3        1 2018-04-03           15      23.3
4        1 2018-05-10           37      23.3
5        2 2018-01-02           NA      50.5
6        2 2018-02-10           39      50.5
7        2 2018-04-13           62      50.5

根据用户评论编辑:

因为你说你有因子而不是字符,只需通过执行以下操作来转换它们:

dat$Buy_date <- as.Date(as.character(dat$Buy_date))
dat$Customer <- as.character(dat$Customer)

【讨论】:

  • 我尝试使用您建议的解决方案,但它显示错误:as.character.factor(x) 中的错误:格式错误的因子。在我的数据框中,这两个字段都是因子。
  • @AKSHMKP 所以通过 as.Date(as.character(x)) 和 as.character(x) 将字段更改为字符
猜你喜欢
  • 1970-01-01
  • 2021-02-18
  • 2015-10-15
  • 1970-01-01
  • 2021-12-14
  • 2020-11-23
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多