Compute sum of all cases - spss

browser pageviews month totalhits percentage
-------------------------------------------------
ch 227025 Nov 2012 626760 36.22
ie 184232 Nov 2012 626760 29.39
s 81430 Nov 2012 626760 12.99
ff 72140 Nov 2012 626760 11.51
ie 39856 Nov 2012 626760 06.36
o 1010 Nov 2012 626760 00.16
rm 325 Nov 2012 626760 00.05
ot 20742 Nov 2012 626760 03.31
I have the above dataset. I want to calculate the percentage based on total hits. However, I want to calculate total hits based on the sum of pageviews. How can I sum multiple cases?
In excell it would be something like SUM(pageviews).

See the AGGREGATE command. Example below:
data list free /browser (A2) pageviews (F6.0) month (A3) year (A4) totalhits (F6.0) percentage (F4.2).
begin data
ch 227025 Nov 2012 626760 36.22
ie 184232 Nov 2012 626760 29.39
s 81430 Nov 2012 626760 12.99
ff 72140 Nov 2012 626760 11.51
ie 39856 Nov 2012 626760 06.36
o 1010 Nov 2012 626760 00.16
rm 325 Nov 2012 626760 00.05
ot 20742 Nov 2012 626760 03.31
end data.
compute const = 1.
AGGREGATE
/OUTFILE=*
MODE=ADDVARIABLES
/BREAK=const
/pageviews_sum = SUM(pageviews).
list vars = pageviews totalhits pageviews_sum.
Which returns in the output:
pageviews totalhits pageviews_sum
227025 626760 626760.0
184232 626760 626760.0
81430 626760 626760.0
72140 626760 626760.0
39856 626760 626760.0
1010 626760 626760.0
325 626760 626760.0
20742 626760 626760.0

Related

How to create dates of a particular month

I want to generate all the dates of a particular month starting from the 1st of that month to the 30 or 31st of that month with out using a trivial for loop:
for i in 1..Time.days_in_month(Date.today.month, Date.today.year)
date = Date.new(y, m, i)
end
I was getting dates like this but I'm not supposed to use this trivial for loop.
Although there are quite a few ways you could accomplish this, one way would be to use a Ruby Range combined with the Time#beginning_of_month and Time#end_of_month methods to generate an object that contains all of the days of the month and then convert it into an array:
(Date.today.beginning_of_month..Date.today.end_of_month).to_a
=> [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019]
Another option would be to use the Time#all_month helper which is as simple as:
Date.today.all_month.to_a
=> [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019]
(Plain Ruby) The Date class uses negative numbers as a way of counting from the end (like in Arrays):
require "date"
today = Date.today
m, y = today.month, today.year
p (Date.new(y,m,1) .. Date.new(y,m,-1)).to_a

Removing lines from files having duplicate date time values.

I am working on parsing a file as follows. I see a duplicate line for "ver."=3 and 5 because there is only 1 second difference (Mon Jan 15 08:24:02 vs Mon Jan 15 08:24:03) software prints it twice.
Also, of you see line for "Ver."5 "Complete Time" has one second difference. I would like delete lines which has rest of the fields matching in the line except "Time" or "Complete Time" column has difference of 1 or 2 seconds.
Loc ID Img Name Ver. Time Complete Time
------------------------------------------------------------------------------------------
ssfad_fs TINT_PAP_1516048511 0 Mon Jan 15 20:35:13 2018 NA
ssfad_fs sfad_jpg 1 Mon Jan 15 18:24:02 2018 Wed Jan 17 18:24:02 2018
ssfad_fs sfad_jpg 1 Mon Jan 15 16:24:02 2018 Wed Jan 17 16:24:02 2018
ssfad_fs sfad_jpg 2 Mon Jan 15 12:24:03 2018 Wed Jan 17 12:24:02 2018
ssfad_fs sfad_jpg 3 Mon Jan 15 08:24:02 2018 Wed Jan 17 08:24:02 2018
ssfad_fs sfad_jpg 3 Mon Jan 15 08:24:03 2018 Wed Jan 17 08:24:02 2018
ssfad_fs sfad_jpg 4 Mon Jan 15 04:24:02 2018 Wed Jan 17 04:24:02 2018
ssfad_fs sfad_jpg 5 Mon Jan 15 00:24:03 2018 Wed Jan 17 00:24:59 2018
ssfad_fs sfad_jpg 5 Mon Jan 15 00:24:03 2018 Wed Jan 17 00:25:00 2018
ssfad_fs sfad_jpg 6 Sun Jan 14 20:24:03 2018 Tue Jan 16 20:24:02 2018
Expected O/P
Loc ID Img Name Ver. Time Complete Time
------------------------------------------------------------------------------------------
ssfad_fs TINT_PAP_1516048511 0 Mon Jan 15 20:35:13 2018 NA
ssfad_fs sfad_jpg 1 Mon Jan 15 18:24:02 2018 Wed Jan 17 18:24:02 2018
ssfad_fs sfad_jpg 1 Mon Jan 15 16:24:02 2018 Wed Jan 17 16:24:02 2018
ssfad_fs sfad_jpg 2 Mon Jan 15 12:24:03 2018 Wed Jan 17 12:24:02 2018
ssfad_fs sfad_jpg 3 Mon Jan 15 08:24:02 2018 Wed Jan 17 08:24:02 2018
ssfad_fs sfad_jpg 4 Mon Jan 15 04:24:02 2018 Wed Jan 17 04:24:02 2018
ssfad_fs sfad_jpg 5 Mon Jan 15 00:24:03 2018 Wed Jan 17 00:24:59 2018
ssfad_fs sfad_jpg 6 Sun Jan 14 20:24:03 2018 Tue Jan 16 20:24:02 2018
When I try #cat junk1.jnk |sort -uk 3,3 command it deletes third line as well which has same Ver.1 number but different times. I want to keep that line. Please help.

Generate random datetimes in rails with the minutes belongs to range(00, 30)

Event model which has start and end datetime attributes in the database. I want to seed some random events but the event time should be proper.
For example:
6.times { date_range << DateTime.now + (rand * 21) }
generates
[Thu, 03 Aug 2017 21:22:48 +0530,
Tue, 08 Aug 2017 17:36:29 +0530,
Sat, 29 Jul 2017 06:19:51 +0530,
Sat, 29 Jul 2017 13:36:21 +0530,
Thu, 27 Jul 2017 15:08:55 +0530,
Fri, 04 Aug 2017 13:53:03 +0530]
which is the correct behaviour.
But how to generate random datetime like this:
[Thu, 03 Aug 2017 21:00:00 +0530,
Tue, 08 Aug 2017 17:30:00 +0530,
Sat, 29 Jul 2017 06:00:00 +0530,
Sat, 29 Jul 2017 13:00:00 +0530,
Thu, 27 Jul 2017 15:30:00 +0530,
Fri, 04 Aug 2017 13:00:00 +0530]
So in order to display these events properly on a calendar.
Could try separating out each segment and adding them onto the date individually
date_range = 6.times.collect do
DateTime.now.beginning_of_day + # starting from today
rand(21).days + # pick a random day, no further than 3 weeks out
rand(24).hours + # move forward to a random hour on that day
(rand(2) * 30).minutes # and then decide whether to add 30 minutes
end
or, could combine the hours + minutes
date_range = 6.times.collect do
DateTime.now.beginning_of_day + # starting from today
rand(21).days + # pick a random day, no further than 3 weeks out
(rand(48) * 30).minutes # pick a random interval of 30 minutes to add in
end
Found the working solution but not complete:
6.times { date_range << DateTime.parse((DateTime.now + (rand * 21)).beginning_of_hour.to_s) }
[Mon, 31 Jul 2017 06:00:00 +0530,
Thu, 03 Aug 2017 15:00:00 +0530,
Fri, 11 Aug 2017 14:00:00 +0530,
Mon, 31 Jul 2017 09:00:00 +0530,
Wed, 09 Aug 2017 16:00:00 +0530,
Sat, 12 Aug 2017 13:00:00 +0530]
This can work for now but need some datetime with 30 minutes as well.

ActiveRecord does not respect daylight saving time (DST)?

We're in the timezone Bern, which is +0100. But since we're now in summertime (we have daylight saving time), the current offset is +0200. In my rails app, I set the timezone using a wrapper in the application controller since I need to have user-based timezones:
around_filter :user_timezone
def user_timezone(&block)
Time.use_zone(current_timezone, &block)
end
Now the strange part:
Time.zone.now # 2013-04-10 10:32:56 +0200
# (correct offset)
SomeArModel.first.created_at # 2013-03-28 17:49:59 +0100
# (incorrect offset, no DST)
Is there any explanation for this?
Thats normal behavior, the DST change happened on Sun Mar 31 01:00:00 UTC 2013.
t = Time.mktime(2013, 03, 31, 1, 15, 0)
6.times do
t += 900
u = Time.at(t.to_i).utc
puts t.to_s + " " + u.to_s
end
output:
Sun Mar 31 01:30:00 +0100 2013 Sun Mar 31 00:30:00 UTC 2013
Sun Mar 31 01:45:00 +0100 2013 Sun Mar 31 00:45:00 UTC 2013
Sun Mar 31 03:00:00 +0200 2013 Sun Mar 31 01:00:00 UTC 2013
Sun Mar 31 03:15:00 +0200 2013 Sun Mar 31 01:15:00 UTC 2013
Sun Mar 31 03:30:00 +0200 2013 Sun Mar 31 01:30:00 UTC 2013
Sun Mar 31 03:45:00 +0200 2013 Sun Mar 31 01:45:00 UTC 2013

Rails 2.3.11 shows a datetime wrong at server

I got this error and makes me crazy.
My desired output is shows the week starting at sunday and after that calculate 10 weeks ago based on that.
Example:
[Sun, 12 Aug 2012, Sun, 05 Aug 2012, Sun, 29 Jul 2012, Sun, 22 Jul
2012, Sun, 15 Jul 2012, Sun, 08 Jul 2012, Sun, 01 Jul 2012, Sun, 24
Jun 2012, Sun, 17 Jun 2012, Sun, 10 Jun 2012, Sun, 03 Jun 2012]
But at my machine it's correctly and return the array above, however at the server is wrong :(
The output at server is:
[Sat, 11 Aug 2012, Sat, 04 Aug 2012, Sat, 28 Jul 2012, Sat, 21 Jul
2012, Sat, 14 Jul 2012, Sat, 07 Jul 2012, Sat, 30 Jun 2012, Sat, 23
Jun 2012, Sat, 16 Jun 2012, Sat, 09 Jun 2012, Sat, 02 Jun 2012]
If I access the app and call the controller from script/console shows wrong but If I recalculate at script/console shows correctly.
My environment:
OS X 10.7.4, ruby 1.8.7 (2012-04-14 patchlevel 361)
[i686-darwin11.3.0], rvm 1.10.2, Seg 13 Ago 2012 09:27:46 BRT (system
date)
And server environment:
Ubuntu 12.04, ruby 1.8.7 (2012-02-08 MBARI 8/0x8770 on patchlevel 358)
[i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2012.02, rvm
1.14.0 (stable), Mon Aug 13 13:29:13 WEST 2012
Probably a time zone mismatch on your server, try setting your time zone explicitly in config/appliction.rb:
config.time_zone = "Pacific Time (US & Canada)"
A popular solution to this issue is setting a before_filter on your controller that configures the right time zone per user. See: http://railscasts.com/episodes/106-time-zones-in-rails-2-1 for a starting point.

Resources