R 按固定次数排序
我希望创建一个增量序列: 我有一个数据框R 按固定次数排序,r,sequence,R,Sequence,我希望创建一个增量序列: 我有一个数据框 dt我们可以使用dplyr并将row_number()添加到组中的第一个ACV new_dt <- dt[rep(seq(1, nrow(dt)), dt$count)] library(dplyr) new_dt %>% group_by(Customer) %>% mutate(ACV = as.numeric(ACV[1]) + row_number() - 1) # Customer count A
dt我们可以使用dplyr
并将row_number()
添加到组中的第一个ACV
new_dt <- dt[rep(seq(1, nrow(dt)), dt$count)]
library(dplyr)
new_dt %>%
group_by(Customer) %>%
mutate(ACV = as.numeric(ACV[1]) + row_number() - 1)
# Customer count ACV
# <chr> <dbl> <dbl>
# 1 a 3 30
# 2 a 3 31
# 3 a 3 32
# 4 b 4 20
# 5 b 4 21
# 6 b 4 22
# 7 b 4 23
# 8 c 5 30
# 9 c 5 31
#10 c 5 32
#11 c 5 33
#12 c 5 34
使用数据表
我们可以
new_dt[, new_ACV := as.integer(ACV[1]) + seq_len(.N) - 1L, by = Customer]
我们可以使用dplyr
并将row\u number()
添加到组中的第一个ACV
new_dt <- dt[rep(seq(1, nrow(dt)), dt$count)]
library(dplyr)
new_dt %>%
group_by(Customer) %>%
mutate(ACV = as.numeric(ACV[1]) + row_number() - 1)
# Customer count ACV
# <chr> <dbl> <dbl>
# 1 a 3 30
# 2 a 3 31
# 3 a 3 32
# 4 b 4 20
# 5 b 4 21
# 6 b 4 22
# 7 b 4 23
# 8 c 5 30
# 9 c 5 31
#10 c 5 32
#11 c 5 33
#12 c 5 34
使用数据表
我们可以
new_dt[, new_ACV := as.integer(ACV[1]) + seq_len(.N) - 1L, by = Customer]
一种使用data.table语法的方法:
# example dataset with ACV as numeric
library(data.table)
dt <- data.table(Customer = c("a", "b", "c"), count = c(3, 4, 5), ACV = c(30,20,30))
# expand the number of rows according to "count"
dt <- dt[rep(1:.N, count)]
# increment ACV
dt[ , ACV := ACV + 1:.N - 1, by=Customer]
#以ACV为数字的示例数据集
库(数据表)
dtdata.table语法的一种方法:
# example dataset with ACV as numeric
library(data.table)
dt <- data.table(Customer = c("a", "b", "c"), count = c(3, 4, 5), ACV = c(30,20,30))
# expand the number of rows according to "count"
dt <- dt[rep(1:.N, count)]
# increment ACV
dt[ , ACV := ACV + 1:.N - 1, by=Customer]
#以ACV为数字的示例数据集
库(数据表)
谢谢。我们能在给定的数字下使增量最大吗。例如,如果我们有季度客户计数季度4 b 4 3 5 b 4 6 b,则不使用ACV457B46`我们能把第六行四分之一换成1吗?@Nik我不清楚你想做什么。你能解释一下吗?我刚回复了我最初的问题谢谢。我们能在给定的数字下使增量最大吗。例如,如果我们有季度客户计数季度4 b 4 3 5 b 4 6 b,则不使用ACV457B46`我们能把第六行四分之一换成1吗?@Nik我不清楚你想做什么。你能解释一下吗?我刚回复了我最初的问题
# example dataset with ACV as numeric
library(data.table)
dt <- data.table(Customer = c("a", "b", "c"), count = c(3, 4, 5), ACV = c(30,20,30))
# expand the number of rows according to "count"
dt <- dt[rep(1:.N, count)]
# increment ACV
dt[ , ACV := ACV + 1:.N - 1, by=Customer]