按周分组数据
Group data by week
我有以下形式的数据。我想按周计算出现次数(即按周分组)。
"CaseNumber" "StartDate" "PatAge" "CallerZip"
"a" "7/4/2017 11:21:00 PM" "10" "12345"
"b" "7/5/2017 3:38:03 PM" "10" "12245"
"c" "7/15/2017 3:38:03 PM" "10" "12245"
我可以在另一个程序中将此文件转换为以下形式,但我想知道是否有办法在 gnuplot 中执行此操作。
Week Count
1 2
2 1
This question suggests it might be easier to convert (and then graph) in another program. My question is different from 因为我无法通过截断 "StartDate"
来获取星期
Gnuplot 有时间格式 %W(参见 help time_specifier
)
%W week of the year (week starts on Monday)
您可以使用它来读取您的日期,使用 strptime() 将字符串从字符串转换为数字秒,然后使用 strftime() 从秒返回到周 #。要查看它是如何工作的,请尝试以下命令序列
date1 = "7/15/2017 3:38:03 PM"
s1 = strptime("%m/%d/%Y", date1)
print s1
1500076800.0
week = strftime("%W", s1)
print week
28
输入函数 timecolumn(column,format) 的工作方式与 strptime 相同,只是它从输入数据列而不是字符串变量中读取。
要在读取数据文件时将所有这些放入单个评估中,最简单的方法是定义一个函数。我将展示如何使用此函数来简单地绘制周数,并留给您将周数用于您想要的其他内容
weekno(column) = int(strftime("%W", timecolumn(column,"\"%m/%d/%Y")))
plot "file" skip 1 using 0:(weekno(2)) with points
备注:
- weekno() 定义中的时间格式包含转义双引号,因为您的数据格式在日期字符串周围有引号。
skip 1
是一种忽略第一行数据中的表头注释的方法
- 您可以在下面的绘图片段中看到,该函数为您的前两个日期返回第 27 周,为第三个日期返回第 28 周。
我理解你的问题,你基本上想创建一个分箱间隔为一周的事件直方图。
您可以使用 smooth freq
执行此操作。检查 help smooth
。
合并间隔将为一周或 3600*24*7
秒。
我稍微修改了你的时间格式。这种带有 AM/PM
"always" 的 12h-time 格式会产生问题,gnuplot 无法读取(还没有,但在下一个版本中。请参阅此处的评论:)。
代码:
### count occurrences by week
reset session
myTimeFmt = '"%m/%d/%Y %H:%M:%S"'
StartDate = '"01/01/2017 00:00:00"'
EndDate = '"12/31/2017 23:59:59"'
# create some test data
# function for creating a random date between two dates
t(date_str) = strptime(myTimeFmt, date_str)
Random_Date(d0,d1) = strftime(myTimeFmt,rand(0)*(t(d1)-t(d0)) + t(d0))
Alphabet = "abcdedfghijklmnopqrstuvwxyz"
set print $Data
do for [i=1:200] {
rand26 = int(rand(0)*26)+1
RandomChar = Alphabet[rand26:rand26]
print sprintf('"%s" %s "%d" "%d"',RandomChar,Random_Date(StartDate,EndDate), \
int(rand(0)*100)+1, int(rand(0)*9e6)+1e6)
}
set print
# print $Data # uncomment if you want to see the random data
set style fill solid 1.0
set boxwidth 0.7
set xlabel "Weeks after start date"
set xtics out
set ylabel "Occurrences per week"
set ytics out
# binning for histogram
bin(n) = floor((timecolumn(n,myTimeFmt)-strptime(myTimeFmt,StartDate))/3600/24/7)+1
# either print a table or directly plot the result
set table $Occurrences
plot $Data u (bin(2)) smooth freq
unset table
print $Occurrences
plot $Data u (bin(2)) smooth freq w boxes notitle
### end of code
结果:
我有以下形式的数据。我想按周计算出现次数(即按周分组)。
"CaseNumber" "StartDate" "PatAge" "CallerZip"
"a" "7/4/2017 11:21:00 PM" "10" "12345"
"b" "7/5/2017 3:38:03 PM" "10" "12245"
"c" "7/15/2017 3:38:03 PM" "10" "12245"
我可以在另一个程序中将此文件转换为以下形式,但我想知道是否有办法在 gnuplot 中执行此操作。
Week Count
1 2
2 1
This question suggests it might be easier to convert (and then graph) in another program. My question is different from "StartDate"
Gnuplot 有时间格式 %W(参见 help time_specifier
)
%W week of the year (week starts on Monday)
您可以使用它来读取您的日期,使用 strptime() 将字符串从字符串转换为数字秒,然后使用 strftime() 从秒返回到周 #。要查看它是如何工作的,请尝试以下命令序列
date1 = "7/15/2017 3:38:03 PM"
s1 = strptime("%m/%d/%Y", date1)
print s1
1500076800.0
week = strftime("%W", s1)
print week
28
输入函数 timecolumn(column,format) 的工作方式与 strptime 相同,只是它从输入数据列而不是字符串变量中读取。 要在读取数据文件时将所有这些放入单个评估中,最简单的方法是定义一个函数。我将展示如何使用此函数来简单地绘制周数,并留给您将周数用于您想要的其他内容
weekno(column) = int(strftime("%W", timecolumn(column,"\"%m/%d/%Y")))
plot "file" skip 1 using 0:(weekno(2)) with points
备注:
- weekno() 定义中的时间格式包含转义双引号,因为您的数据格式在日期字符串周围有引号。
skip 1
是一种忽略第一行数据中的表头注释的方法- 您可以在下面的绘图片段中看到,该函数为您的前两个日期返回第 27 周,为第三个日期返回第 28 周。
我理解你的问题,你基本上想创建一个分箱间隔为一周的事件直方图。
您可以使用 smooth freq
执行此操作。检查 help smooth
。
合并间隔将为一周或 3600*24*7
秒。
我稍微修改了你的时间格式。这种带有 AM/PM
"always" 的 12h-time 格式会产生问题,gnuplot 无法读取(还没有,但在下一个版本中。请参阅此处的评论:
代码:
### count occurrences by week
reset session
myTimeFmt = '"%m/%d/%Y %H:%M:%S"'
StartDate = '"01/01/2017 00:00:00"'
EndDate = '"12/31/2017 23:59:59"'
# create some test data
# function for creating a random date between two dates
t(date_str) = strptime(myTimeFmt, date_str)
Random_Date(d0,d1) = strftime(myTimeFmt,rand(0)*(t(d1)-t(d0)) + t(d0))
Alphabet = "abcdedfghijklmnopqrstuvwxyz"
set print $Data
do for [i=1:200] {
rand26 = int(rand(0)*26)+1
RandomChar = Alphabet[rand26:rand26]
print sprintf('"%s" %s "%d" "%d"',RandomChar,Random_Date(StartDate,EndDate), \
int(rand(0)*100)+1, int(rand(0)*9e6)+1e6)
}
set print
# print $Data # uncomment if you want to see the random data
set style fill solid 1.0
set boxwidth 0.7
set xlabel "Weeks after start date"
set xtics out
set ylabel "Occurrences per week"
set ytics out
# binning for histogram
bin(n) = floor((timecolumn(n,myTimeFmt)-strptime(myTimeFmt,StartDate))/3600/24/7)+1
# either print a table or directly plot the result
set table $Occurrences
plot $Data u (bin(2)) smooth freq
unset table
print $Occurrences
plot $Data u (bin(2)) smooth freq w boxes notitle
### end of code
结果: