Extract date from string with another numbers from R - date

I need to extract the date from this text:
Mellisoni 2014 Malbec (Columbia Valley (WA))
Okapi 2013 Estate Cabernet Sauvignon (Napa Valley)
Podere dal Nespoli 2015 Prugneto Sangiovese (Romagna)
Simonnet-Febvre 2015 Chablis
Lagler 2012 1000 Eimerberg Smaragd Neuburger (Wachau)
I use this code:
vino<-mutate(vino, year1=sub("^.*([0-9]{4}).*", "\\1", vino$title))
It works, but I have the last value extract on 1000 instead of 2012, how can I fix it if have another numbers?

Related

parse javascript date to elixir format

I have some saved dates in JavaScript using new Date() that looks like:
"Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)"
I'm trying to parse these to Elixir DateTime; I didn't find anything in "timex" that can help and I already know that I can use DateTime.from_iso8601 but for dates saved using new Date().toISOString() but what i need is to parse the above string.
Thanks in advance
You can use elixir binary pattern matching to extract the date parts and parse using Timex's RFC1123 format. The RFC1123 is the format e.g Tue, 05 Mar 2013 23:25:19 +0200. Run h Timex.Format.DateTime.Formatters.Default in iex to see other formats.
iex> date_string = "Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)"
iex> <<day_name::binary-3,_,month_name::binary-3,_,day::binary-2,_,year::binary-4,_,time::binary-8,_::binary-4,offset::binary-5,_,rest::binary>> = date_string
iex> Timex.parse("#{day_name}, #{day} #{month_name} #{year} #{time} #{offset}", "{RFC1123}")
iex> {:ok, #DateTime<2019-02-24 14:44:20+02:00 +02 Etc/GMT-2>}
Pattern matching:
The binary-size are in byte sizes. 1 byte == 1 character. For instance to get
3-character day_name the size is 3. Underscores (_) is used to pattern match the spaces in the date format
Updated answer to use binary-size rather than bitstring-size for simplicity
I didn't find anything in "timex" that can help
The Timex Parsing docs say that you can use strftime sequences, e.g %H:%M:%S, for parsing. Here's a list of strftime characters and what they match.
Here's a format string that I think should work on javascript Dates:
def parse_js_date() do
Timex.parse!("Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)",
"%a %b %d %Y %H:%M:%S GMT%z (%Z)",
:strftime)
end
Unfortunately, %Z doesn't want to match the time zone name, which causes Timex.parse!() to spit out an error. It looks like %Z in Elixir only matches one word, e.g. a timezone abbreviation EET. Therefore, my simple, clean solution is spoiled.
What you can do is chop off the time zone name before parsing the date string:
def parse_js_date_string() do
[date_str|_tz_name] = String.split(
"Sun Feb 24 2019 14:44:20 GMT+0200 (Eastern European Standard Time)",
" (",
parts: 2
)
Timex.parse!(date_str,
"%a %b %d %Y %H:%M:%S GMT%z",
:strftime)
end
In iex:
~/elixir_programs/my$ iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Compiling 1 file (.ex)
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.parse_js_date_string()
#DateTime<2019-02-24 14:44:20+02:00 +02 Etc/GMT-2>
iex(2)>

Chosing specific dates/hours from an array

I have a matrix that has 3months of data or so..Its a 952x1 matrix with the elements in the following format(3 hourly )
Aug-05-2015 03:00:00
Aug-05-2015 06:00:00
Aug-05-2015 09:00:00
Aug-05-2015 12:00:00
Aug-05-2015 15:00:00
Aug-05-2015 18:00:00
Aug-05-2015 21:00:00
Aug-06-2015 00:00:00
Aug-06-2015 03:00:00
Aug-06-2015 06:00:00
I would want to choose say only day timings/ only night or say for august month alone. How do i do that.
Further to my question, if I have a group of .wav files and Im trying to pick only month wise or do daily psd averages etc or chose files belonging to a month how to go about? The following are first 10 .wav files in a .txt file that are read into matlab code-
AMAR168.1.20150823T200235Z.wav
AMAR168.1.20150823T201040Z.wav
AMAR168.1.20150823T201845Z.wav
AMAR168.1.20150823T202650Z.wav
AMAR168.1.20150823T203455Z.wav
AMAR168.1.20150823T204300Z.wav
AMAR168.1.20150823T205105Z.wav
AMAR168.1.20150823T205910Z.wav
AMAR168.1.20150823T210715Z.wav
yyyymmddTHHMMSSZ.wav is part of the format to get sense of some parameters.
Thanks.
Are these datetimes? If so, you can use logical indexing here if you make use of some of the datetime functions. To get the times in August:
t = datetime(2015, 8, 1, 3, 0, 0) + hours(3:3:3000)';
t(month(t) == 8) % Times in August
To get the times that are during the day or night:
t(hour(t) < 12) % Day times
t(hour(t) >= 12) % Night times

Automatically put working week

Right now i am working on a weekly basis gathering the data and put the week and month manually. For example: The working week for today this week is June 23 thru June 29. and the month is June 2014.
I want to gather the YTD data and based on the date put the Week and Month automatically
For example:
Referral Request Date Week Month
1/3/2014 0:00 December 30 thru January 05, 2014 January 2014
1/3/2014 11:10 December 30 thru January 05, 2014 January 2014
12/31/2013 0:00 December 30 thru January 05, 2014 December 2013
6/18/2014 0:00 June 16 thru June 22, 2014 June 2014
6/20/2014 9:51 June 16 thru June 22, 2014 June 2014
4/28/2014 16:34 April 28 thru May 04, 2014 April 2014
5/1/2014 15:22 April 28 thru May 04, 2014 May 2014
The working week will begin each monday and finished on Sunday.
It can be do automatically?? The file have thousand of lines...
Here you are:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use DateTime;
use DateTime::Format::Strptime;
my #datetimes = (
'1/3/2014 0:00',
'1/3/2014 11:10',
'12/31/2013 0:00',
'6/18/2014 0:00',
'6/20/2014 9:51',
'4/28/2014 16:34',
'5/1/2014 15:22',
);
for my $datetime_str (#datetimes) {
my $strp = 'DateTime::Format::Strptime'->new( pattern => '%m/%d/%Y %H:%M' );
my $dt = $strp->parse_datetime($datetime_str);
my $month_year_strp = 'DateTime::Format::Strptime'->new( pattern => '%B %Y' );
my $month_year = $month_year_strp->format_datetime($dt);
my $desired_dow = 1; # Monday
$dt->subtract(days => ($dt->day_of_week - $desired_dow) % 7);
my $month_day_strp = 'DateTime::Format::Strptime'->new( pattern => '%B %d' );
my $monday = $month_day_strp->format_datetime($dt);
$dt->add(days => 6);
my $sunday = $month_day_strp->format_datetime($dt);
say "$datetime_str, $monday thru $sunday, $month_year";
}
Next time help someone else (in case this is what you're after -- I am not sure if I got your question). :-) I used the link posted by #scragar in comments.

Dealing with (birthday) dates and Timezone with Dojo

I have a simple widget that has:
<input class="input" id="${id}_dateOfBirth" name="dateOfBirth" data-dojo-type="dijit/form/DateTextBox" />
Note that it's a birthday. So, it's meant to stay the same, regardless of where you are when you are (timezone shouldn't happen). If you are born on the 10th of January at 3:00AM in England, and view your personal information from New York, you are meant to still see 10th of January, NOT the 9th!
I am in GMT+8 right now.
When I submit this form, this actually gets to the server when I put in 1/1/1970:
dateOfBirth: "1969-12-31T16:00:00.000Z"
Which is bad, because it's 8 hours short of the actual date.
Basically, I need a way for the DateTextBox to show the date as it came from the server, effectively ignoring the browser's timezone.
FWIW, here is my variant of UTCDateTextBox:
define([
"dojo/_base/declare",
"dijit/form/DateTextBox"
], function(declare, DateTextBox) {
function isValidDate(value) {
return value instanceof Date && isFinite(value.getTime());
}
function toUTCDate(value) {
if (isValidDate(value)) {
value = new Date(
Date.UTC(value.getFullYear(), value.getMonth(), value.getDate())
);
}
return value;
}
return declare(DateTextBox, {
_getValueAttr : function() {
return toUTCDate(this.inherited("_getValueAttr", arguments));
}
});
});
For my use case, I found that I didn't need to override _setValueAttr(). With the above implementation, when getUTCXXX(), toUTCString(), toISOString() or toJSON() are called on the date object returned from _getValueAttr(), then the correct UTC date with zeroed time elements is returned.
Hope this helps.
After much hacking and analysing Dojo's source code, I came up with this:
var UTCDateTextBox = declare( 'UTCDateTextBox', [ DateTextBox ], {
_getValueAttr: function(){
var ov = this.inherited(arguments);
if( ov ){
ov.setTime( ov.getTime() - ov.getTimezoneOffset() * 60 * 1000 );
}
return ov;
},
_setValueAttr: function( value, priorityChange, formattedValue){
var v = stamp.fromISOString( value );
if( v ){
v.setTime( v.getTime() + v.getTimezoneOffset() * 60 * 1000 );
value = v;
}
this.inherited(arguments);
}
});
Basically:
When the value is set, the timezone difference gets added. This means that if the server has 1979-12-25T00:00:00.000Z, rather than assigning Tue Dec 25 1979 08:00:00 GMT+0800 (WST), it will assign Tue Dec 25 1979 00:00:00 GMT+0800 (WST) . Basically, the date is converted locally to whatever it was in UTC.
When the value is parsed, it will be changed from Tue Dec 25 1979 00:00:00 GMT+0800 (WST) to Tue Dec 25 1979 08:00:00 GMT+0800 (WST)
The changed value is the one submitted to the server. So, it will be correct regardless of what timezone it will be edited at.
Since I only ever ever deal with dates, if the server has 1979-12-31T23:00:00Z (which is an error: for birthdays, the time is actually ignored and mustn't matter), this will happen:
When the value is set, ISO is 1979-12-31T23:00:00.000Z. So, Tue Jan 01 1980 07:00:00 GMT+0800 (WST) is changed into Mon Dec 31 1979 23:00:00 GMT+0800 (WST). This means that the right date is placed into the date textbox (31/12/1979).
When the value is parsed from the textbox, Mon Dec 31 1979 00:00:00 GMT+0800 (WST) becomes Mon Dec 31 1979 08:00:00 GMT+0800 (WST). So, the server will save 1979-12-31T00:00:00Z -- which is, again, the correct date!
If there are bettere solutions, please let me know. Frankly, I hope there are as this one feels like a bit of a cheat!

MATLAB Cut 3D array of daily data into monthly segments

I have a 1437x159x1253 large matrix (let's call it A) of daily sea ice data for a little over 2 years. I need to write a code that takes the daily data from each month and does mean(A, 3) on it. So basically, 1253 is the t in days. If I start from January, I need to do mean(A,3) of the first 31 days, then the mean(A,3) of February, the next 28 or 29 days. Because the days alternate between 31 and 30 (and 28 or 29 for February), I don't know how to write a code to do this. I can do it manually, but that would take a while.
Thanks!
You can initialize an array containing the number of days in each month, Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] using boolean to check whether it's a leap year (to set Mon(2) = 29). The number of days will help you index each month appropriately, using a loop like:
index=1;
for i=1:12
average = mean(A(:,:,index:(index+Mon(i)-1),3);
index = index+M(i); % Starting location of the next month
end