大佬教程收集整理的这篇文章主要介绍了计算之前未发生的新值,而不是在最后一组中发生的值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
原始数据看起来像
library(dplyr) date <- c("2010-01-10","2010-02-13","2010-03-22","2010-01-11","2010-02-14","2010-03-23","2010-01-12","2010-03-24") mth <- rep(c("2010-01","2010-02","2010-03"),3) user <- c("123","129","145","123","180","184","145") dt <- data.frame(date,mth,user) dt <- dt %>% arrange(datE) dt date mth user 1 2010-01-10 2010-01 123 2 2010-01-11 2010-01 123 3 2010-01-12 2010-01 180 4 2010-02-13 2010-02 129 5 2010-02-14 2010-02 129 6 2010-02-14 2010-02 184 7 2010-03-22 2010-03 145 8 2010-03-23 2010-03 180 9 2010-03-24 2010-03 145
答案应该是这样的
new <- c(2,2,1,1) totNew <- c(2,4,5,5) notLastMonth <- c(2,2) tmp <- cbind(dt,new,totNew,notLastMonth) tmp date mth user new totNew notLastMonth 1 2010-01-10 2010-01 123 2 2 2 2 2010-01-11 2010-01 123 2 2 2 3 2010-01-12 2010-01 180 2 2 2 4 2010-02-13 2010-02 129 2 4 2 5 2010-02-14 2010-02 129 2 4 2 6 2010-02-14 2010-02 184 2 4 2 7 2010-03-22 2010-03 145 1 5 2 8 2010-03-23 2010-03 180 1 5 2 9 2010-03-24 2010-03 145 1 5 2
dt %>% group_by(user) %>% mutate(Count = row_number()) %>% # Count appearances per user group_by(mth) %>% mutate(new = sum(count == 1)) %>% # Count first appearances per months summarise(new = first(new),# Summarise new users per month (for cumsum) users = list(unique(user))) %>% # Create a list of unique users per month (for notLastMonth) mutate(totNew = cumsum(new),# Calculate overall cummulative sum of unique users notLastMonth = lengths(Map(setdiff,users,lag(users)))) %>% # Compare new users to prevIoUs month SELEct(-users) %>% right_join(dt) # Join BACk to the real data # A tibble: 9 × 6 # mth new totNew notLastMonth date user # <fctr> <int> <int> <int> <fctr> <fctr> # 1 2010-01 2 2 2 2010-01-10 123 # 2 2010-01 2 2 2 2010-01-11 123 # 3 2010-01 2 2 2 2010-01-12 180 # 4 2010-02 2 4 2 2010-02-13 129 # 5 2010-02 2 4 2 2010-02-14 129 # 6 2010-02 2 4 2 2010-02-14 184 # 7 2010-03 1 5 2 2010-03-22 145 # 8 2010-03 1 5 2 2010-03-23 180 # 9 2010-03 1 5 2 2010-03-24 145
以上是大佬教程为你收集整理的计算之前未发生的新值,而不是在最后一组中发生的值全部内容,希望文章能够帮你解决计算之前未发生的新值,而不是在最后一组中发生的值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。