Change format to hh:mm:sss - arrays

Can I adapt this formula to make the result into hh:mm:ss format instead of just a raw number?
Get hours from schedule
=ARRAYFORMULA(
SUM(
IFERROR(
IF(
--REGEXEXTRACT(A2:G2,"- (\d+:\d+)")<(--REGEXEXTRACT(A2:G2,"^(\d+:\d+)")),
1+REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)"),
REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)")))*
24))

try:
=ARRAYFORMULA(TEXT(SUM(IFERROR(IF(
--REGEXEXTRACT(A2:G2,"- (\d+:\d+)")<(
--REGEXEXTRACT(A2:G2,"^(\d+:\d+)")),
1+REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)"),
REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)")))*24), "[hh]:mm:ss"))

Suggestion
Based on your question & on your previous post, you want to sum up the time from range A2:G2 OR AR2:AX2 (as seen on your sample screenshot) in hh:mm:ss format. Perhaps you can try this tweaked function below:
=ARRAYFORMULA(TEXT(SUM(IFERROR(TIMEVALUE(TRANSPOSE(
{
ARRAYFORMULA(IFNA(REGEXEXTRACT(AR2:AX2,"- (\d+:\d+)"),0)),
ARRAYFORMULA(IFNA(REGEXEXTRACT(AR2:AX2,"^(\d+:\d+)"),0))
}
)))), "[hh]:mm:ss"))
This function was derived from an answer on this quite similar post:
Demonstration

Related

how do I filter my values using an array?

I am trying to sift through some free text answers on geographical locations. As one of the steps, I want to check if the answer is any of the 290 municipalities in my country. As 290 entries would make my code cumbersome/hard to read I try saving them in an array, like below:
Data resorTEST;
keep R_res_ort_namn R_res_ort_txt R_kom_lan R_kommun;
set resor1TEST resor2TEST resor3TEST;
R_res_ort_namn=strip(lowcase(R_res_ort_namn));
R_res_ort_txt=strip(lowcase(R_res_ort_txt));
R_kom_lan=strip(lowcase(R_kom_lan));
array kommuner{290} $ ("upplands väsby" "vallentuna" "österåker" "värmdö"
"järfälla" "ekerö" "huddinge" "botkyrka" "salem"
"haninge" "tyresö" "upplands-bro" "nykvarn" "täby"
"danderyd" "sollentuna" "stockholm" "södertälje" "nacka" "sundbyberg"
"solna" "lidingö" "vaxholm" "norrtälje" "sigtuna" "nynäshamn"
"håbo" "älvkarleby" "knivsta" "heby" "tierp" "uppsala"
"enköping" "östhammar" "vingåker" "gnesta" "nyköping" "oxelösund" "flen"
"katrineholm" "eskilstuna" "strängnäs" "trosa" "ödeshög" "ydre"
"kinda" "boxholm" "åtvidaberg" "finspång" "valdemarsvik" "linköping"
"norrköping" "söderköping" "motala" "vadstena" "mjölby" "aneby"
"gnosjö" "mullsjö" "habo" "gislaved" "vaggeryd" "jönköping"
"nässjö" "värnamo" "sävsjö" "vetlanda" "eksjö" "tranås"
"uppvidinge" "lessebo" "tingsryd" "alvesta" "älmhult" "markaryd"
"växjö" "ljungby" "högsby" "torsås" "mörbylånga" "hultsfred"
"mönsterås" "emmaboda" "kalmar" "nybro" "oskarshamn" "västervik"
"vimmerby" "borgholm" "gotland" "olofström" "karlskrona" "ronneby"
"karlshamn" "sölvesborg" "svalöv" "staffanstorp" "burlöv" "vellinge"
"östra göinge" "örkelljunga" "bjuv" "kävlinge" "lomma" "svedala"
"skurup" "sjöbo" "hörby" "höör" "tomelilla" "bromölla"
"osby" "perstorp" "klippan" "åstorp" "båstad" "malmö"
"lund" "landskrona" "helsingborg" "höganäs" "eslöv" "ystad"
"trelleborg" "kristianstad" "simrishamn" "ängelholm" "hässleholm" "hylte"
"halmstad" "laholm" "falkenberg" "varberg" "kungsbacka" "härryda"
"partille" "öckerö" "stenungsund" "tjörn" "orust" "sotenäs"
"munkedal" "tanum" "dals-ed" "färgelanda" "ale" "lerum"
"vårgårda" "bollebygd" "grästorp" "essunga" "karlsborg" "gullspång"
"tranemo" "bengtsfors" "mellerud" "lilla edet" "mark" "svenljunga"
"herrljunga" "vara" "götene" "tibro" "töreboda" "göteborg"
"mölndal" "kungälv" "lysekil" "uddevalla" "strömstad" "vänersborg"
"trollhättan" "alingsås" "borås" "ulricehamn" "åmål" "mariestad"
"lidköping" "skara" "skövde" "hjo" "tidaholm" "falköping"
"kil" "eda" "torsby" "storfors" "hammarö" "munkfors"
"forshaga" "grums" "årjäng" "sunne" "karlstad" "kristinehamn"
"filipstad" "hagfors" "arvika" "säffle" "lekeberg" "laxå"
"hallsberg" "degerfors" "hällefors" "ljusnarsberg" "örebro" "kumla"
"askersund" "karlskoga" "nora" "lindesberg" "skinnskatteberg" "surahammar"
"kungsör" "hallstahammar" "norberg" "västerås" "sala" "fagersta"
"köping" "arboga" "vansbro" "malung-sälen" "gagnef" "leksand"
"rättvik" "orsa" "älvdalen" "smedjebacken" "mora" "falun"
"borlänge" "säter" "hedemora" "avesta" "ludvika" "ockelbo"
"hofors" "ovanåker" "nordanstig" "ljusdal" "gävle" "sandviken"
"söderhamn" "bollnäs" "hudiksvall" "ånge" "timrå" "härnösand"
"sundsvall" "kramfors" "sollefteå" "örnsköldsvik" "ragunda" "bräcke"
"krokom" "strömsund" "åre" "berg" "härjedalen" "östersund"
"nordmaling" "bjurholm" "vindeln" "robertsfors" "norsjö" "malå"
"storuman" "sorsele" "dorotea" "vännäs" "vilhelmina" "åsele"
"umeå" "lycksele" "skellefteå" "arvidsjaur" "arjeplog" "jokkmokk"
"överkalix" "kalix" "övertorneå" "pajala" "gällivare" "älvsbyn"
"luleå" "piteå" "boden" "haparanda" "kiruna");
/*if not missing(R_res_ort_namn) then R_kommun=prxchange("s/^.*-(.* kommun)/$1/",1,R_res_ort_namn);
else if prxmatch("/^.*([a-zA-Z]*? kommun).*$/",R_res_ort_txt) then R_kommun=prxchange("s/^.*?([a-zA-Z]*? kommun).*$/$1/",-1,R_res_ort_txt);
else if prxmatch("/^.*([a-zA-Z]*? kommun).*$/",R_kom_lan) then R_kommun=prxchange("s/^.*?([a-zA-Z]*? kommun).*$/$1/",-1,R_kom_lan);
else */if R_res_ort_txt in kommuner then R_kommun=R_res_ort_txt;
run;
However, for some reason this does not seem to work for all of the municipalities. The municipality of "uppsala" works for instance, but not the municipality of "ängelholm".
I have tried stripping the variables of whitespace and converting everything to lowercase. What am I doing wrong?
Additional info:
For some reason it does work flawlessly if I skip the array and just copy-paste the exact same list of municipality names into a parenthesis following the in-operator. I would however need to repeat this step 5-6 times and this solution would make my code quite cumbersome.
You are defining the ARRAY
Array kommuner{290} $
with character length 8. See what happens when you fix that.

lapply calling .csv for changes in a parameter

Good afternoon
I am currently trying to pull some data from pushshift but I am maxing out at 100 posts. Below is the code for pulling one day that works great.
testdata1<-getPushshiftData(postType = "submission", size = 1000, before = "1546300800", after= "1546200800", subreddit = "mysubreddit", nest_level = 1)
I have a list of Universal Time Codes for the beginning and ending of each day for a month. What I would like to do is get the syntax to replace the "after" and "before" values for each day and for each day to be added to the end of the pulled data. Even if it placed the data to a bunch of separate smaller datasets I could work with it.
Here is my (feeble) attempt. "links" is the data frame with the UTCs
mydata<- lapply(1:30, function(x) getPushshiftData(postType = "submission", size = 1000, after= links$utcstart[,x],before = links$utcendstart[,x], subreddit = "mysubreddit", nest_level = 1))
Here is the error message I get: Error in links$utcstart[, x] : incorrect number of dimensions
I've also tried without the "function (x)" argument and get the following message:
Error in ifelse(is.null(after), "", sprintf("&after=%s", after)) :
object 'x' not found
Can anyone help with this?

When creating a tensor with an array of timestamps, the numbers are incorrect

Looking for some kind of solution to this issue:
trying to create a tensor from an array of timestamps
[
1612892067115,
],
but here is what happens
tf.tensor([1612892067115]).arraySync()
> [ 1612892078080 ]
as you can see, the result is incorrect.
Somebody pointed out, I may need to use the datatype int64, but this doesn't seem to exist in tfjs 😭
I have also tried to divide my timestamp to a small float, but I get a similar result
tf.tensor([1.612892067115, 1.612892068341]).arraySync()
[ 1.6128920316696167, 1.6128920316696167 ]
If you know a way to work around using timestamps in a tensor, please help :)
:edit:
As an attempted workaround, I tried to remove my year, month, and date from my timestamp
Here are my subsequent input values:
[
56969701,
56969685,
56969669,
56969646,
56969607,
56969602
]
and their outputs:
[
56969700,
56969684,
56969668,
56969648,
56969608,
56969600
]
as you can see, they are still incorrect, and should be well within the acceptable range
found a solution that worked for me:
Since I only require a subset of the timestamp (just the date / hour / minute / second / ms) for my purposes, I simply truncate out the year / month:
export const subts = (ts: number) => {
// a sub timestamp which can be used over the period of a month
const yearMonth = +new Date(new Date().getFullYear(), new Date().getMonth())
return ts - yearMonth
}
then I can use this with:
subTimestamps = timestamps.map(ts => subts(ts))
const x_vals = tf.tensor(subTimestamps, [subTimestamps.length], 'int32')
now all my results work as expected.
Currently only int32 is supported with tensorflow.js, your data has gone out of the range supported by int32.
Until int64 is supported, this can be solved by using a relative timestamp. Currently a timestamp in js uses the number of ms that elapsed since 1 January 1970. A relative timestamp can be used by using another origin and compute the difference of ms that has elapsed since that date. That way, we will have a lower number that can be represented using int32. The best origin to take will be the starting date of the records
const a = Date.now() // computing a tensor out of it will give an accurate result since the number is out of range
const origin = new Date("02/01/2021").now()
const relative = a - origin
const tensor = tf.tensor(relative, undefined, 'int32')
// get back the data
const data = tensor.dataSync()[0]
// get the initial date
const initial date = new Date(data + origin)
In other scenarios, if using the ms is not of interest, using the number of s that has elapsed since the start would be better. It is called the unix time

Adding an array to an existing csv file

I know this question was already asked, but after trying most of the accepted answer none of them seems to work with my simple task...
I have a csv file as follow:
Date,Median
2000-01-31,9
2000-02-28,8
2000-03-31,7
2000-04-30,6
2000-05-31,5
2000-06-30,4
2000-07-31,3
2000-08-31,2
2000-09-30,1
2000-10-31,0
2000-11-30,11
2000-12-31,12
and then an array:
[0.1829 0.171349 0.162461 0.152306 0.14122 0.137749 0.138802 0.150315
0.156784 0.168297 0.180634 0.187241]
I wish to append this array as a third column to the csv file to get the following output:
Date,Median,Median2
2000-01-31,9,0.1829
2000-02-28,8,0.171349
2000-03-31,7,0.162461
2000-04-30,6,0.152306
2000-05-31,5,0.14122
2000-06-30,4,0.137749
2000-07-31,3,0.138802
2000-08-31,2,0.150315
2000-09-30,1,0.156784
2000-10-31,0,0.168297
2000-11-30,11,0.180634
2000-12-31,12,0.187241
I tried most of the answer related to this kind of question but I did not succeed to make them work..here is the last code I tried, using pandas that looks easier but it does not work:
data=pd.read_csv("data_1.csv",sep=',')
array_transpose = array.reshape((-1, 1)) #in order to transpose the array
data['Median2'] = data[array_transpose]
data.to_csv('output.csv')
which produce the following error:
KeyError: '[0.1829 0.171349 0.162461 0.152306 0.14122 0.137749 0.138802 0.150315\n 0.156784 0.168297 0.180634 0.187241] not in index'
How to append this array to my csv file?
You may not need reshape
data=pd.read_csv("data_1.csv",sep=',')
data['Median2'] = array
data.to_csv('output.csv')

How to make ISO8601 produce Hebrew day of the year, week of the year?

Can someone think of a way to calculate the Day-of-the-Year and Week-of-the-Year for the Hebrew calendar? I'm using ISO 8601 which can return the Hebrew year, month, day of the month and day of the week. I'm trying to write re-usable code to calculator any Torah portion (Parsha).
Take a look at the hebcal command to do this. There are also ports for JavaScript (shameless plug, it was written by me), Java, and Perl.
This is easy if you can use Java. My library Time4J enables the features you want (in Hebrew calendar):
PlainDate iso8601 = Iso8601Format.parseDate("2018-03-16");
HebrewCalendar hc = iso8601.transform(HebrewCalendar.axis());
int hyear = hc.getYear();
HebrewMonth hmonth = hc.getMonth(); // obtains an enum
int hdayOfMonth = hc.getDayOfMonth();
Weekday hdayOfWeek = hc.getDayOfWeek(); // obtains an enum
int hdayOfYear = hc.getDayOfYear();
int weekOfYear =
hc.get(
CommonElements.weekOfYear(
HebrewCalendar.axis(),
HebrewCalendar.getDefaultWeekmodel()
)
);
System.out.println("year=" + hyear); // 5778
System.out.println("month=" + hmonth); // ADAR_II (technical result, see next line)
System.out.println(
"month=" + hmonth.getDisplayName(Locale.ENGLISH, hc.isLeapYear())); // Adar
System.out.println("day-of-month=" + hdayOfMonth); // 29
System.out.println("day-of-week=" + hdayOfWeek); // FRIDAY
System.out.println("day-of-year=" + hdayOfYear); // 207
System.out.println("week-of-year=" + weekOfYear); // 30
There are many more features, for example taking into account start of day in the evening of previous day (including astronomical calculations of sunset) or hebrew biblical clock time or different numbering schemes for months etc.

Resources