咒语之间的平均时间间隔
average timespread between spells
我有一个数据框如下,我想根据事件的最大传播创建一个时间传播变量。
A<- c('1244', '1244', '1244', '1245', '1245', '1245', '1245', '1245', '1245', '1245')
sequence<- c(1,1,0, 1,1,0,0,1,1,1)
# 1= lived locally and 0 lived internationally
date<- c('19/Oct/12', '19/Oct/12', '20/Oct/12', '19/Oct/11', '19/Oct/11', '22/Nov/12',
'24/Nov/12', '29/Nov/12','2/Dec/12', '29/Dec/12')
df<- data.frame(A,sequence, date)
我想计算这些人在本地和国际地点的平均时间分布。例如,如果我们看到人 1244 she/he 的序列为 110(即,他们在本地生活了两个法术,在国际上生活了 1 个法术,如果我们想计算时间分布,则 avg_local_timespread= 总时间spells/count 法术中的事件转化为 0 day/2(这是最后日期 - 开始日期之间的差异(因此,0 天 = 19/Oct/12-19/Oct/12)和 avg_international_timespread= 0/1=0 (20/Oct/12-20/Oct/12)
对于 1245 人来说,它是 1100111,所以他们在本地生活了两个时间段,第一个时间段是 11,第二个时间段是 111。因为我想在这种情况下获得本地的平均时间差,所以它是时差第一个法术 (11) + 最后一个法术 (111)/总法术 (5) 的开始日期和最终日期之间。因此 avg_local_timespread= 6 天 (30+0)/5 和 avg_international_timespread= 1 天 (2/2)('24/Nov/12'-'22/Nov/12' )
我期待如下输出:
A avg_local_timespread avg_international_timespread total_local_timespread
1244 0 0 0
1245 6 1 30
total_international_timespread
1244 0
1245 2
我无法复制您想要的输出,因为我不理解您在问题中提供的计算。但我相信这会让你入门。
library(data.table)
library(lubridate)
# convert to data.table format
setDT(df)
# convert dates to date-format
df[, date := lubridate::dmy(date, locale = "English_United States")]
# create sequence-groups by A
df[, seq_group := rleid(sequence == 1), by = .(A)][]
# summarise
ans <- df[, .(start = min(date), end = max(date)), by = .(A, seq_group, sequence)]
# add diration of spell
ans[, duration := end - start]
# A seq_group sequence start end duration
# 1: 1244 1 1 2012-10-19 2012-10-19 0 days
# 2: 1244 2 0 2012-10-20 2012-10-20 0 days
# 3: 1245 1 1 2011-10-19 2011-10-19 0 days
# 4: 1245 2 0 2012-11-22 2012-11-24 2 days
# 5: 1245 3 1 2012-11-29 2012-12-29 30 days
我有一个数据框如下,我想根据事件的最大传播创建一个时间传播变量。
A<- c('1244', '1244', '1244', '1245', '1245', '1245', '1245', '1245', '1245', '1245')
sequence<- c(1,1,0, 1,1,0,0,1,1,1)
# 1= lived locally and 0 lived internationally
date<- c('19/Oct/12', '19/Oct/12', '20/Oct/12', '19/Oct/11', '19/Oct/11', '22/Nov/12',
'24/Nov/12', '29/Nov/12','2/Dec/12', '29/Dec/12')
df<- data.frame(A,sequence, date)
我想计算这些人在本地和国际地点的平均时间分布。例如,如果我们看到人 1244 she/he 的序列为 110(即,他们在本地生活了两个法术,在国际上生活了 1 个法术,如果我们想计算时间分布,则 avg_local_timespread= 总时间spells/count 法术中的事件转化为 0 day/2(这是最后日期 - 开始日期之间的差异(因此,0 天 = 19/Oct/12-19/Oct/12)和 avg_international_timespread= 0/1=0 (20/Oct/12-20/Oct/12)
对于 1245 人来说,它是 1100111,所以他们在本地生活了两个时间段,第一个时间段是 11,第二个时间段是 111。因为我想在这种情况下获得本地的平均时间差,所以它是时差第一个法术 (11) + 最后一个法术 (111)/总法术 (5) 的开始日期和最终日期之间。因此 avg_local_timespread= 6 天 (30+0)/5 和 avg_international_timespread= 1 天 (2/2)('24/Nov/12'-'22/Nov/12' )
我期待如下输出:
A avg_local_timespread avg_international_timespread total_local_timespread
1244 0 0 0
1245 6 1 30
total_international_timespread
1244 0
1245 2
我无法复制您想要的输出,因为我不理解您在问题中提供的计算。但我相信这会让你入门。
library(data.table)
library(lubridate)
# convert to data.table format
setDT(df)
# convert dates to date-format
df[, date := lubridate::dmy(date, locale = "English_United States")]
# create sequence-groups by A
df[, seq_group := rleid(sequence == 1), by = .(A)][]
# summarise
ans <- df[, .(start = min(date), end = max(date)), by = .(A, seq_group, sequence)]
# add diration of spell
ans[, duration := end - start]
# A seq_group sequence start end duration
# 1: 1244 1 1 2012-10-19 2012-10-19 0 days
# 2: 1244 2 0 2012-10-20 2012-10-20 0 days
# 3: 1245 1 1 2011-10-19 2011-10-19 0 days
# 4: 1245 2 0 2012-11-22 2012-11-24 2 days
# 5: 1245 3 1 2012-11-29 2012-12-29 30 days