Get date from string Kusto - azure-data-explorer

Can someone tell me why this does not work?
let x = "July, 2021";
let calculatedTime = replace_string(x,","," 01");
print(todatetime(calculatedTime))
while this works?
let y = todatetime("July 01 2021");
print y
How to get 1st day of the month from a string like "July, 2021"?

The reason that you are seeing this discrepancy is that the constant value in your second query is being parsed by the query string parser using .Net library that supports this format, while the first expression is being evaluated by the query engine which is using different libraries, these libraries supports the datetime formats specified here.
Here is workaround for this issue, most likely this can be written simpler:
let GetMonthNumber = view(Month:string){
case(
Month=="January", "01",
Month=="February", "02",
Month=="March", "03",
Month=="April", "04",
Month=="May", "05",
Month=="June", "06",
Month=="July", "07",
Month=="August", "08",
Month=="September", "09",
Month=="October", "10",
Month=="November", "11",
Month=="December", "12",
"-1" // default case is an error
)};
let x = "July, 2021";
let dateparts = split(x, ",");
let calculatedTime = strcat(replace_string(tostring(dateparts[1]), " ", ""),"-", GetMonthNumber(tostring(dateparts[0])), "-01");
print todatetime(calculatedTime)
print_0
2021-07-01 00:00:00.0000000

Related

How to plot cohorts in R

I am trying to plot a cohort trace of 15 health states in R but I dont really know how to do it in the right way.
My data looks like this:
dput(m_M_ad)
structure(c(0.27, 0.256305347853185, 0.242951797354611, 0.230338299921634,
0.217887258961913, 0.206025667287424, 0.194528979719196, 0.183521237213434,
0.172613550573761, 0.162125923204547, 0.151911608290387, 0.141973282939457,
0.132384528668431, 0.123063114917053, 0.11409239073185, 0.105133571796206,
0.0964203968902034, 0.0880524400334065, 0.0796819390545833, 0.0715595854274743,
0.0637832780760189, 0.0562452782961504, 0.048895299564326, 0.0420162775188823,
0.0355429328075438, 0.0296701003100819, 0.0241478188660469, 0.0193789874288362,
0.015053609850195, 0.0114393543650247, 0.00850240847915511, 0.00611969003581926,
0.00425688548697882, 0.00286069846863142, 0.00184377068125287,
0.00115832820003068, 0.73, 0.694258518269723, 0.659309431031376,
0.626239989358266, 0.59349048593319, 0.56222592658017, 0.531841994942455,
0.502682882175171, 0.473690367815364, 0.445743656866086, 0.418445265742517,
0.391806302085698, 0.366033618174396, 0.340904804524758, 0.316654434816042,
0.292347251027103, 0.268632863795214, 0.245792040526907, 0.222858984287112,
0.200534154983283, 0.179095252475684, 0.158244724923299, 0.137844248925194,
0.118693709948007, 0.10061576242418, 0.0841678950816436, 0.0686504682655951,
0.0552138569891875, 0.0429873358711903, 0.0327420661761112, 0.0243934768369293,
0.0176003713912805, 0.0122738786801316, 0.00826985442880786,
0.00534462216383268, 0.00336710531708768, 0, 0.00635, 0.0123249090324111,
0.0179378598991147, 0.0231694678489822, 0.0280447303732529, 0.0325497905192035,
0.0367035848654766, 0.040439424918347, 0.0438053809687469, 0.0467677583196886,
0.0493183254852193, 0.0514740374755558, 0.0532036790499603, 0.0545344606685087,
0.0553072778008801, 0.0556004711086501, 0.0554588516511904, 0.054666580475368,
0.0533417235912645, 0.0515432246481583, 0.0491880384974129, 0.0462162536830676,
0.0428676003665985, 0.0391088869990321, 0.0351768664658957, 0.0308506501675402,
0.0266613975675514, 0.0223199580028192, 0.018280536217857, 0.014646595679713,
0.0113750146607816, 0.00854788116817462, 0.00621359424947056,
0.0043402964971858, 0.00295740756408225, 0, 0.003175, 0.00616245451620557,
0.00896892994955736, 0.0115847339244911, 0.0140223651866265,
0.0162748952596017, 0.0183517924327383, 0.0202197124591735, 0.0219026904843735,
0.0233838791598443, 0.0246591627426097, 0.0257370187377779, 0.0266018395249801,
0.0272672303342543, 0.02765363890044, 0.027800235554325, 0.0277294258255952,
0.027333290237684, 0.0266708617956323, 0.0257716123240792, 0.0245940192487064,
0.0231081268415338, 0.0214338001832992, 0.019554443499516, 0.0175884332329479,
0.0154253250837701, 0.0133306987837757, 0.0111599790014096, 0.00914026810892852,
0.00732329783985649, 0.00568750733039081, 0.00427394058408731,
0.00310679712473528, 0.0021701482485929, 0.00147870378204113,
0, 0.00127, 0.00246498180648223, 0.00358757197982294, 0.00463389356979643,
0.00560894607465059, 0.00650995810384069, 0.00734071697309532,
0.00808788498366939, 0.00876107619374939, 0.00935355166393771,
0.00986366509704387, 0.0102948074951112, 0.0106407358099921,
0.0109068921337017, 0.011061455560176, 0.01112009422173, 0.0110917703302381,
0.0109333160950736, 0.0106683447182529, 0.0103086449296317, 0.00983760769948257,
0.00924325073661352, 0.0085735200733197, 0.00782177739980641,
0.00703537329317915, 0.00617013003350804, 0.00533227951351027,
0.00446399160056385, 0.00365610724357141, 0.0029293191359426,
0.00227500293215632, 0.00170957623363492, 0.00124271884989411,
0.00086805929943716, 0.00059148151281645, 0, 0.00254, 0.00495373842795219,
0.00724416447943338, 0.00941799765783645, 0.0114765276655505,
0.0134250821878605, 0.0152668820966222, 0.0170063328098263, 0.0186441677477521,
0.0201841587543024, 0.021628695718949, 0.0229802014548782, 0.0242418068059007,
0.0254158688746185, 0.026505547307178, 0.027510776096417, 0.0284337234115682,
0.0292775172527557, 0.0300419629775482, 0.0307292696292247, 0.0313425932464801,
0.0318840638095113, 0.032355333505619, 0.0327607860355906, 0.0331041892916691,
0.0333912054830727, 0.0336250976950681, 0.0338130413587618, 0.033959230469905,
0.0340704720197173, 0.0341532686073078, 0.0342129481102336, 0.0342545234095418,
0.0342825059122739, 0.0343005702393266, 0, 0.000692346, 0.00134412344655775,
0.00195672806876768, 0.00252801068096107, 0.00306066343458451,
0.00355314150949529, 0.00400747924737695, 0.00441636473408992,
0.00478501307171198, 0.00510971345719726, 0.00538953475998585,
0.00562629837313592, 0.0058165629823512, 0.0059632727736761,
0.00604900199267857, 0.00608228125124658, 0.00606798125054011,
0.00598245874170456, 0.00583859400377125, 0.00564280712439055,
0.00538597952584612, 0.00506151969398995, 0.00469564824350054,
0.00428471061386547, 0.00385462466300522, 0.00338117983443763,
0.00292257103289611, 0.00244711437859726, 0.00200460229907916,
0.00160640112694151, 0.00124780740242907, 0.000937848487830812,
0.000681861181339916, 0.000476378362535685, 0.000324655473916315,
0, 0.000963264, 0.00187008479521078, 0.00272240426958982, 0.00351723225177192,
0.00425831434376975, 0.00494350123060215, 0.00557562330069836,
0.00614450745612511, 0.00665740949107753, 0.00710916654914402,
0.00749848314432814, 0.00782789338871085, 0.00809260936674949,
0.00829672733728848, 0.00841600277242236, 0.00846230434956046,
0.00844240869640363, 0.00832342085802373, 0.00812326122263826,
0.0078508620861086, 0.00749353673161199, 0.00704211435685558,
0.00653307581704423, 0.00596133650624761, 0.00536295605287683,
0.00470425020443496, 0.00406618578489894, 0.00340468087457009,
0.002789011894371, 0.00223499287226645, 0.00173607986424914,
0.00130483267872113, 0.000948676426212058, 0.00066278728700617,
0.000451694572405307, 0, 0.00030102, 0.00058440149850337, 0.000850751334246819,
0.00109913507867873, 0.00133072323242805, 0.00154484413456317,
0.00174238228146824, 0.0019201585800391, 0.00208044046596173,
0.0022216145466075, 0.00234327598260254, 0.00244621668397214,
0.00252894042710922, 0.00259272729290265, 0.00263000086638199,
0.00264447010923765, 0.00263825271762613, 0.00260106901813242,
0.00253851913207446, 0.00245339440190894, 0.00234173022862875,
0.00220066073651737, 0.00204158619282632, 0.00186291765820238,
0.00167592376652401, 0.00147007818888593, 0.00127068305778092,
0.00106396277330315, 0.000871566216990938, 0.000698435272583265,
0.000542524957577857, 0.000407760212100353, 0.000296461383191268,
0.000207121027189428, 0.000141154553876659, 0, 0.00105357, 0.00205526683209511,
0.00300626336368029, 0.00390929969537093, 0.00476486444794536,
0.00557512525789805, 0.00634137695638695, 0.00706540914033929,
0.00774748566063782, 0.00838913612193228, 0.00899131662505551,
0.00955499843030602, 0.0100814495552015, 0.0105716170440972,
0.0110267844116588, 0.0114468897086337, 0.0118328045584424, 0.0121858031997845,
0.0125057717435305, 0.0127936021318254, 0.0130505841656891, 0.013277579615128,
0.0134752511477536, 0.0136454079994793, 0.0137896036441917, 0.01391018923149,
0.0140085115235269, 0.0140875638046839, 0.0141490900281738, 0.0141959364315846,
0.0142308256531358, 0.0142559897967222, 0.0142735318242161, 0.0142853466722778,
0.0142929792784183, 0, 0.00554, 0.0107535747211902, 0.015652091423646,
0.0202185489776578, 0.0244746728930785, 0.0284082888779771, 0.0320358421920919,
0.0352990490891639, 0.0382397829833271, 0.0408285546849597, 0.0430580986394221,
0.044943139059886, 0.0464563478156613, 0.0476214079695113, 0.0482993141884705,
0.0485583867412685, 0.0484376829310943, 0.0477486208032232, 0.0465942245689797,
0.0450259026159578, 0.0429710428032106, 0.0403772277070668, 0.0374538142017196,
0.0341717555224214, 0.0307378656641582, 0.0269591153240065, 0.023299614734674,
0.0195067033838936, 0.0159773229777886, 0.0128019532272718, 0.00994296832277849,
0.00747217819573439, 0.00543195760250571, 0.00379452862928521,
0.00258567760670601, 0, 0.001135, 0.0022137165400495, 0.00323745366711279,
0.00420920110635351, 0.00512952248072957, 0.00600078690824088,
0.00682442237276213, 0.00760238711075745, 0.00833499780443345,
0.00902393034610636, 0.00967024302428446, 0.0102750092507793,
0.0108396196619564, 0.0113651191388567, 0.0118529121597705, 0.0123029597684919,
0.0127162232276225, 0.0130940939281995, 0.0134364758210685, 0.013744349354193,
0.0140191195237827, 0.0142617321661502, 0.0144729193644219, 0.0146546374906482,
0.0148085676522837, 0.0149372406978305, 0.0150421128943951, 0.0151263952325276,
0.0151919629831741, 0.0152418640808977, 0.0152790111704534, 0.0153057910768984,
0.015324450283809, 0.0153370111859407, 0.0153451214641265, 0,
0.001135, 0.00220309275461207, 0.00320661056724482, 0.00414207375790905,
0.00501393649462151, 0.00581970783030296, 0.00656275986086454,
0.00723115567880555, 0.00783347652951274, 0.0083636843914829,
0.00882029395607766, 0.00920632442583286, 0.00951618060427556,
0.00975471668386883, 0.00989346162477442, 0.00994641349383308,
0.0099215755435554, 0.00978032281638279, 0.0095437615478721,
0.00922242459093158, 0.00880144175044886, 0.00827007974061339,
0.00767122237917321, 0.00699892215370464, 0.00629553982622289,
0.00552154089291908, 0.00477198237469738, 0.0039951158307429,
0.00327223837022667, 0.00262187872720241, 0.00203632854248913,
0.00153029240489488, 0.00111244607539997, 0.000777097988120394,
0.00052952651992041, 0, 0.0173, 0.0335862930174351, 0.0488937548417712,
0.0631686826826854, 0.0764783466912671, 0.08878414566455, 0.10013691272806,
0.110353941381557, 0.119565544020789, 0.127678996931466, 0.134671033482905,
0.140587165745525, 0.145341403856851, 0.14900731568406, 0.151149475079425,
0.151981040760784, 0.151623719403801, 0.149486725180024, 0.14589190414221,
0.140999678270629, 0.134582197047629, 0.126474754972263, 0.117332539817605,
0.107064233230022, 0.0963174578462074, 0.0844872522348232, 0.0730277619414321,
0.0611472858220204, 0.0500900124707436, 0.0401399581944405, 0.0311795952630952,
0.0234344949482961, 0.0170380105282338, 0.0119035073097373, 0.00811233068256658,
0, 0.00798093387709187, 0.0172221342253082, 0.0261571268761119,
0.0370239778724022, 0.0480847928139013, 0.0602397578542127, 0.0729061053037527,
0.0879097532689816, 0.103772954507293, 0.121228981040427, 0.140308286316361,
0.160628742635701, 0.182670905097199, 0.205955818516762, 0.232674304512434,
0.261490416150404, 0.291761099892009, 0.326045858051948, 0.362710854324399,
0.401035697341258, 0.441902106311621, 0.485843087451169, 0.530383701240229,
0.575951489659739, 0.620414603209111, 0.665993555491638, 0.708048258677768,
0.74942326221472, 0.786436630178053, 0.818593010075497, 0.846594003866054,
0.870075701935559, 0.88894441816401, 0.903706818735331, 0.914363263232678
), dim = c(36L, 15L), dimnames = list(c("0", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35"), c("Healthy_noAC",
"Healthy_AC", "IS_mild", "IS_mod", "IS_sev", "IS_dead", "IH_mild",
"IH_mod", "IH_sev", "IH_dead", "MI", "MI_dead", "SE", "MB", "Dead"
)))
I tried using plot_trace(m_M_ad)
The problem is that the plot shows the last two health states as white. How can I change the color?
The trace plotted with plot_trace(m_M_ad)
Does anyone know how to add different colors to that plot? Or maybe how to write a code for plotting that cohort trace another way?
Very thankful for any help!

Is there an R library or function for formatting international currency strings?

Here's a snippet of the JSON data I'm working with:
{
"item" = "Mexican Thing",
...
"raised": "19",
"currency": "MXN"
},
{
"item" = "Canadian Thing",
...
"raised": "42",
"currency": "CDN"
},
{
"item" = "American Thing",
...
"raised": "1",
"currency": "USD"
}
You get the idea.
I'm hoping there's a function out there that can take in a standard currency abbreviation and a number and spit out the appropriate string. I could theoretically write this myself except I can't pretend like I know all the ins and outs of this stuff and I'm bound to spend days and weeks being surprised by bugs or edge cases I didn't think of. I'm hoping there's a library (or at least a web api) already written that can handle this but my Googling has yielded nothing useful so far.
Here's an example of the result I want (let's pretend "currency" is the function I'm looking for)
currency("USD", "32") --> "$32"
currency("GBP", "45") --> "£45"
currency("EUR", "19") --> "€19"
currency("MXN", "40") --> "MX$40"
Assuming your real json is valid, then it should be relatively simple. I'll provide a valid json string, fixing the three invalid portions here: = should be :; ... is obviously a placeholder; and it should be a list wrapped in [ and ]:
js <- '[{
"item": "Mexican Thing",
"raised": "19",
"currency": "MXN"
},
{
"item": "Canadian Thing",
"raised": "42",
"currency": "CDN"
},
{
"item": "American Thing",
"raised": "1",
"currency": "USD"
}]'
with(jsonlite::parse_json(js, simplifyVector = TRUE),
paste(raised, currency))
# [1] "19 MXN" "42 CDN" "1 USD"
Edit: in order to change to specific currency characters, don't make this too difficult: just instantiate a lookup vector where "USD" (for example) prepends "$" and appends "" (nothing) to the raised string. (I say both prepend/append because I believe some currencies are always post-digits ... I could be wrong.)
pre_currency <- Vectorize(function(curr) switch(curr, USD="$", GDP="£", EUR="€", CDN="$", "?"))
post_currency <- Vectorize(function(curr) switch(curr, USD="", GDP="", EUR="", CDN="", "?"))
with(jsonlite::parse_json(js, simplifyVector = TRUE),
paste0(pre_currency(currency), raised, post_currency(currency)))
# [1] "?19?" "$42" "$1"
I intentionally left "MXN" out of the vector here to demonstrate that you need a default setting, "?" (pre/post) here. You may choose a different default/unknown currency value.
An alternative:
currency <- function(val, currency) {
pre <- sapply(currency, switch, USD="$", GDP="£", EUR="€", CDN="$", "?")
post <- sapply(currency, switch, USD="", GDP="", EUR="", CDN="", "?")
paste0(pre, val, post)
}
with(jsonlite::parse_json(js, simplifyVector = TRUE),
currency(raised, currency))
# [1] "?19?" "$42" "$1"

How to get 'species score' for ordination with metaMDS()?

I am using metaMDS() from Vegan to make an ordination. My data is samples (rows) times variables (columns). In my case the columns are analytes measured i blood, i.e. not 'species' per se. Due to the different scales for my variables I have transformed the data using scale(center = T, scale = T).
It is a numeric matrix with no missing values and no binary variables.
But the function does not calculate scores for 'species' (i.e. my analytes).
And I get the message 'species scores not available'.
I can't find anywhere that these scores should not be calculated when using 'euclidean' distance. I normally get the species score when I use Bray-Curtis distance on a regular community dataset.
plotdf <- structure(c(-0.698904932895234, 0.30704395903274, 1.24113935868014,
2.30332141847988, -0.723897451949346, 0.122724131008669, 0.160212909589836,
0.0446225089645731, 0.39451777572213, -0.105332605360096, -0.892596955564596,
0.338284607850377, 0.178957298880421, 0.15396477982631, 1.61602714449181,
-0.292776498265928, -0.814495333520497, -0.533329494161748, -0.336513406610622,
-0.211550811340068, 3.13524832588293, 2.26119814124526, 1.70180602307717,
1.80669204523371, 3.13524832588293, 1.87661606000469, 1.52699598614965,
2.12135011170326, -1.06718496185488, -1.13361277588734, -0.601897693913475,
-0.328503392807931, 1.46276186573425, 0.00347540139166063, -0.745695995144315,
-0.749246570483348, -0.76344887183948, -0.775875885526096, -0.807831063577394,
0.0869139218589377, -0.832685090950625, -0.0923901327622313,
-0.587695392557344, -0.770550022517546, -0.0107268999644709,
-0.525560324124265, -0.427919502300856, -0.798954625229811, -0.806055775907877,
-0.310750516112765, 3.44444636497001, 0.200147425204428, -0.0244578860101126,
0.200147425204428, -0.36968456806209, -0.398800071367679, -0.120123111157047,
0.408115305958629, -0.890990722485962, -0.961699801942393, -0.273778277609555,
0.41093290575329, 3.181624670989, 3.76548692036815, -0.406474243377552,
0.416240744384011, 0.00222933118786958, -0.427705597900429, -0.268470438978834,
-1.12834029715544, -0.517938854622665, -0.783330786158653, -0.661250497652098,
-0.862948365619448, 0.947024607455987, -0.204776375410201, -0.581632918191303,
-0.348088018439634, 0.368470196707535, -0.188852859518039, 0.865085267987317,
0.286121529607417, 0.570778700977531, 0.382615486004067, 0.821662987608825,
0.44533655766189, 1.25106109357391, 2.8818089566773, -1.44594498771245,
-1.13716432724317, -1.44345540764024, 0.543632464272209, -0.889117589295565,
0.151331854366745, 0.765367591610085, 0.577745560785727, 0.245142869778918,
-1.26436165094427, -0.326251496822513, -1.02556997534963, -0.974400330579358,
-0.778250025626633, 0.466877997116795, -0.539458350031998, -1.07673962011992,
0.867706881150637, 0.0745773872113305, -0.104516369484636, -1.57990779369432,
-0.360364593336031, 0.485255827280768, 3.57337992412814, 0.256505894180964,
-0.264535620101922, 2.13733867744602, 1.79421377779631, 1.52733885584654,
2.07379702936274, -1.15411869326783, 1.20963061543015, -0.278018853200573,
0.884992040049025, 2.17533989241356, 1.52167683562364, 0.137948546574831,
-0.278018853200573, 0.154926807790159, -0.176149285908647, -0.575138424468722,
-0.69398625297599, -0.278018853200573, -0.626073208114692, -0.626073208114692,
-0.736431906014289, 0.307731158728054, -0.294997114415901, -0.855279734521558,
-0.311975375631229, -0.430823204138483, -0.227084069554603, 2.88551074988959,
2.47142262330214, 1.68465518278598, 2.11254624692636, 3.60326350264118,
3.56185468998243, 2.47142262330214, 2.73367843680753, -0.675647138762483,
-0.38578545015127, -0.294373188387428, 2.29466328872395, 6.49068792404239,
2.82139829613626, -0.0622526766464104, 0.535904026686221, 0.0180967312639449,
-0.705047939929233, -0.642553955998955, -0.142602084556756, -0.0711803886364486,
-0.633626244008917, -0.937168451670251, 0.160940123104569, -0.294373188387428,
0.321638938925279, -1.08001184351088, -0.749686499879426, -0.517565988138408,
-0.553276836098561, 0.54926502031515, 1.38672720907941, -0.444646148767711,
-0.987616139285201, -0.407834623986865, -0.49066055474377, -0.923195970918719,
-0.38022598040123, -0.242182762473055, 0.107526722944989, -1.46680807598847,
2.62643635730582, 1.68823363847608, -1.17998610194623, -0.992345558180284,
-0.831510806380897, -2.25489835980544, -0.724287638514648, -1.45876633839851,
-0.295394967049624, 0.428361416047591, -0.375812342949318, 0.669613543746671,
1.17892359111139, 2.46560160550643, 0.750030919646365, -1.77775526280061,
-0.751093430481203, -0.456229718849011, 0.696419335713228, 0.378429058462806,
0.84201157835516, 0.713238656162837, -0.832036410144966, 0.919275331670544,
-0.162417214744905, 0.0178648763243318, 0.172392382955099, -0.342699305814142,
-0.445717643567996, -1.1097816715345, 0.730197996765378, 6.53377023116585,
1.26110122121555, -0.855239029674838, 0.104750362481623, 0.221112713046046,
-0.484334037250749, -0.549787859443237, -1.13887225917561, -0.54251521253296,
-0.498879331071302, -0.964328733328984, 0.301111829059086, 0.337475063610461,
0.0902050686610707, -1.16069019990644, -1.21305325766043, -0.0261572819033443,
-0.797057854392627, -0.140787338747868, 0.441771092401777, -0.470669823856697,
-0.751420875013156, -1.16552867546893, -1.00409682105396, -0.582970244319283,
-0.681233112224038, -0.877758848033557, -0.639120454550571, -0.167969655559799,
2.19329729357675, 5.38160698124336, 4.37477234303285, -0.359747681885611,
0.982698502395062, 0.587156323098077, -0.371733808530972, -0.395706061821705,
-1.59911317701617, -0.563511834856784, -0.851178874345507, -0.659400848019695,
-0.347761555240249, -0.155983528914438, 1.30632392181987, -0.87515112763623,
-0.44365056840315, -0.155983528914438, -0.503581201629967, -0.0537715590349406,
2.47365169616714, 0.150652380724054, 0.531624268456718, 0.420120301315449,
-0.193151517961524, 0.410828304053678, 1.05197611511598, -0.416159452244061,
-0.769255348191411, -0.309794112249109, -0.304271920832414, -0.297815438433252,
-0.297131810885106, -0.309361148135283, -0.307667270988209, -0.308829437820058,
-0.30965738673948, -0.310135926023183, 0.243389703849444, -0.0870302777547278,
-0.307849571667715, -0.309581428123019, -0.308821841958412, -0.307089985503107,
0.0481760595453695, -0.149316343252526, -0.108298690363732, -0.211602408750324,
-0.309323168827053, 1.63254216944839, 0.17253410335763, 0.235865204597817,
-0.283223643067293, -0.304586580610635, -0.306690077901827, -0.306576986649612,
-0.304925854367279, 0.0662283262758862, -0.301442643799068, 0.929605567553241,
2.25902865198899, 4.95029977413941, 2.64812809133604, -0.47547574119999,
-0.432242470161435, -0.302542657045751, -0.464667423440349, -0.464667423440349,
-0.399817516882512, -0.670025460873512, 0.270298184215185, 0.270298184215185,
1.19981351154425, 1.17819687602497, 0.194639959897707, -1.28609957317301,
-0.637600507594599, 0.389189679571229, -0.432242470161435, -0.350014484068485,
0.215408884220155, -0.423765358193087, -0.890854227648913, -0.841686978232511,
-0.534391669379991, -0.645017980566895, -0.595850731150493, -0.80481154117021,
-0.0918864246323654, -0.48374453836672, 4.17862597661995, 5.68911318449899,
3.32268322548848, -0.257171457184862, 0.563526592429426, -0.513954282524298,
-0.77073710786374, -0.977170359607208, -0.443464879489945, -1.01241506112439,
0.0701007711889329, -0.322625902859619, -0.0708780348797788,
0.910938650241606, -0.221926755667683, -0.513954282524298, -0.322625902859619,
0.03989102703135, 0.0902406006273229, -0.0648244445283561, 0.391240413183369,
0.285285951290747, -0.253699789641292, -0.659090774273934, -0.742011657494247,
-0.566956459584699, -0.461001997692076, -0.682124352946243, -0.258306505375753,
-0.420693217666615, 2.40995896415691, 6.42553531511589, 1.48835127705159,
0.161894498824987, 0.665487270707522, 0.994632873245161, 0.105939746393572,
-0.23308022422017, -1.90513988511128, -0.697175523798217, 0.33634166816991,
0.764230951468825, 0.402170788677427, 0.145437218698094, 0.402170788677427,
-0.279160608575437, -0.256120416397798, -0.341698273057587, 0.435085348931185,
0.838074051864222, 1.31633692302975, 0.59894261628146, 0.412951499717062,
0.0409692665883208, -0.657825928503537, 0.333241021189475, 0.120679745115908,
-0.362897157951458, -0.171592009485246, -0.333650770025627, 2.21948894199296,
5.12305959487686, 1.43519112196112, -0.150091705762848, 0.634206114269011,
0.433959862345984, -0.450461083647381, -0.617332960249911, -1.13463577771773,
-0.68408171089092, 1.18488330705733, 0.70095486491002, 0.667580489589508,
0.1502776721217, 0.433959862345984, 9.29831794179632e-05, -0.0332813921410786,
0.116903296801188, 0.484021425326744, 0.807251285728501, 1.62776898417899,
0.652436625643507, 1.10139913989, -0.245488402849498, -0.601562121044994,
0.265399975431007, -0.322895732891995, -0.245488402849498, -0.183562538815489,
-0.171864707173939, 2.88790201225057, 6.25364540361752, 2.77316076027215,
-0.22923533316314, 0.382718010721768, 0.382718010721768, -0.611706173091208,
-0.66907679908041, -1.18541243298329, -0.860312219044444, -0.496964921112788,
-0.22923533316314, 0.172359048761322, 0.688694682664205, 0.191482590757734,
-0.649953257084015, -0.343976585141561, -0.210111791166746, 0.975547812610265,
0.222389325297373, 0.851437956724518, 0.303036585736753, -0.713118895799399,
-0.632471635360019, -1.26152026678716, -0.616342183272147, -0.584083279096402,
-1.03570793755692, -0.245364785251013, 0.00895755191294446, 0.984545704690042,
7.5829220077053, 2.82642375246424, -0.248095758051148, 0.559496924434463,
1.77392201087898, 0.377333161467775, 0.680939433078909, -1.05366439872601,
-0.826971715923037, -0.235951507186702, -0.00521074076224157,
-0.110460914920767, -0.0477156187878014, 0.336852325252967, -0.493004817150792,
-0.177254294675216, -0.316913179616336, -0.377634433938563, -0.422757412167952,
0.295225804994338, -0.0721144456468375, -0.289179139207527, -0.416078498519931,
-0.753363637745003, -0.514592474828243, -0.436115239463994, -0.850207885641312,
-0.239087286847367, -0.00656190873037701, 2.43718215979915, 5.49186224546105,
2.79088195919157, -0.199489072035347, 0.73299221727198, 1.05453748944691,
0.379292417879529, 0.34713789066204, -0.476018006105799, -0.00656190873037701,
-0.0708709631653894, 0.15421072735707, 0.73299221727198, 0.700837690054491,
0.443601472314542, -0.919750481707214, 0.218519781792082, 1.1831555983169,
-0.482448911549298, -0.344507148209773, 0.458909211725853, -0.116463203249896,
-0.463791673265714, 0.0729886894860142, 0.318574476365909, -0.312931832753788,
0.318574476365909, -1.04618082500943, -0.0322623620339359, -0.0384066209005535,
1.53648495801046, 6.52705048079341, 3.16250938039262, 0.749039168554954,
0.45246867642236, 0.697906325083822, -0.2429379947851, -0.263391132173553,
-0.935276695384289, -0.3554302504216, -0.760402370712999, -0.723586723413778,
0.186577890372456, -0.212258288702411, 1.35240672151437, -1.11321899066384,
-1.02015721554638, -0.488375643446552, -0.212258288702411, -0.517689783976593,
-0.517689783976593, -0.404074729996812, -0.716516128441214, -0.678644443781284,
-1.01380885302165, -0.574497310966489, -0.12950501621233, -0.77332365543111,
-0.527157705141571, -0.6329761214973, 2.18318173522514, 4.28249941023639,
0.954312852291706, 0.391081280947224, 0.595892761436129, 1.00551572241394,
0.365479845886108, 0.109465495274971, -0.376961770886163, -0.095345985213934,
1.3383343782084, 1.49194298857507, 1.3383343782084, 0.672697066619453,
-0.095345985213934, -1.62119151485626, -0.607374686436184, 0.467885586130548,
1.38953724833063, -0.651224895277883, 0.155618947409365, -1.0668717233289,
-1.18912079040272, -1.11577135015842, -0.82237358918124, -1.26247023064701,
-0.895723029425547, -1.0668717233289, 0.0333698803355454, -0.052069378070287,
2.73328153457067, 6.5440088820849, 2.54963202384708, -0.113285881644804,
0.162188384440567, 0.529487405887713, -0.453037476483425, -0.296935392368385,
-1.05295921151378, -0.624443686492099, -0.401003448445078, -0.312239518262021,
1.41712670771834, 0.238709013908721, 1.26408544878204, -0.928995791775366,
-0.945830330258361, -0.251023014687489, 0.0397553772915032, -0.592177487733025,
-0.00842427829955308, -0.251023014687489, -0.607339908757271,
-0.856003613554907, -1.00459533959252, -0.763512845307005, -0.53456028784089,
-0.402647224929949, -0.251023014687489, 0.227980242429395, 2.40547844522623,
3.95993525473729, 3.4564674621831, 1.24750252235161, 1.12792892161999,
1.00206197348145, 0.668514560914304, 0.580407697217322, -0.0363403486615505,
-0.137033907172389, 0.523767570554978, 0.731448034983579, -0.181087339020876,
-0.470581319739529, -0.653088394540424, -0.395061150856403, 0.0580598624423623,
0.397900622416433, 0.888781720156762, -0.584336071479825, -0.50609378010719,
-0.40377693754298, -0.295441457180876, -1.1861998512693, -1.37277762300404,
-0.993603441736668, -0.722764740831404, 0.420776440768595, 0.637447401492804,
-0.272315269292895, 2.66941297149803, 7.41532733360265, 3.99900765660126,
0.157956344080792, 0.361088865416019, 1.2474853221515, 0.638087758145851,
0.619621165297205, -0.807846461903923, -0.111655911509586, -0.407121397088085,
-0.259388654298836, -0.0673360886728128, -0.364648233536176,
-0.146742437922033, -0.67858031196333, -0.370188211390772, -0.120889207933915,
0.342622272567355, -0.0607210092437259, 0.0586382429485912, -0.0802880997670564,
-0.246608369215364, -0.248565078267698, -0.589032453373648, -0.305309640785355,
-0.117465571761385, -0.497067127913995, -0.178123552383708, 0.0562843265198969,
1.72321561241497, 4.55261213715794, 1.83288214438177, 0.297550696846829,
0.385283922420244, 0.9555498886475, -0.47011502692064, -0.711381397247572,
-0.996514380361199, 0.385283922420244, 0.911683275860804, 0.385283922420244,
0.802016743894006, 0.363350616026884, 0.626550292747176, 0.122084245699975,
0.780083437500669, 0.451083841600322, 0.9555498886475, 0.318768231003561,
0.715510556440497, 0.219582649644338, 0.358442463547259, -0.157322559520749,
-0.593739117501383, 0.219582649644338, 0.0608857194695682, -0.375530838511066,
-0.196996792064447, -0.652950143413879, 0.625186447640068, 1.98637884605493,
2.40165788285948, -0.832904392695844, -0.558358807252847, -0.648335931893829,
-0.846747027255997, -0.922881517336828, -0.703706470134434, -0.969023632537331,
-0.309191385170126, -0.496066951732166, -0.323034019730277, 0.902039138843094,
-0.043874222767227, -0.355333500370629, -0.0392600112471769,
-0.392247192531032, -1.17897025669963, 2.3237306833112, 3.55022035107835,
1.63689646936162, 1.85766460955969, 3.82004807798711, 3.30492241752492,
1.6859560560723, 1.53877729594025, 0.255869103455813, -0.217555908302303,
0.147043929627318, 1.33013687809435, 1.4455605803838, 2.80178908228503,
-0.689777911971317, 1.73411983610745, 0.810730217791754, -0.545498284109474,
0.0316202273378633, -0.834057539833125, -0.776345688688416, 0.724162441074655,
-0.920625316550224, -0.89176939097787, -0.603210135254218, -0.862913465405515,
0.233611706344417, 0.781874292219364, -0.0838034749516248, -0.603210135254218,
-0.630192299425754, 0.643965453120263, -0.855043667522133, -0.292915247281239,
-1.07989503561849, -1.37969685974697, -0.855043667522133, -1.11737026363455,
0.344163628991805, -0.705142755457904, -0.421112829288472, 1.33922283110583,
5.51499186229142, 4.4040074411503, -0.559028136740472, -0.622239319322639,
-0.421112829288472, -0.250634185354747, -0.354070665943748, -0.817619338212975,
-0.854013655457253, -0.664380107710751, -0.662464617329474, -0.281282031455192,
0.0194499584054227, 0.0941540832752574, -0.827196790119363, -0.817619338212975,
-0.409619887000803, -0.597337944366027, -0.0832549257485097,
0.204960720543472, -0.143804431272036, -0.170446213702386, -0.158336312597682,
-0.20435393679556, 0.277620127171702, 0.476222505288867, -0.642732356785886,
-0.591870772146124, -1.49887064253455, -1.22501683268422, -1.6774709533065,
-0.955131918628824, -0.133570489077839, 1.93025532428696, 0.945969167143749,
0.473670567546803, 0.362541485288701, 1.69212157659102, -0.645558046624104,
2.56527865147613, 1.53336574479373, 1.1483828526853, 2.48590073557747,
0.0251853427194558, 0.807057814321117, 0.830871189090712, 1.49367678684442,
-0.0740370521538533, 0.951792873536318, 2.5032023290554, 0.318564524344853,
0.635178698940584, -0.241842564689589, 0.125429877841457, -0.685102409123612,
0.375555075772084, 0.188752712760605, 0.217247988474217, -0.423761492139935,
0.962684741533591, 4.03514176906493, 4.24637318970771, -0.813579477507975,
-0.264377783836745, -0.441044062919797, -0.723326052324239, -0.560101772736638,
-1.00752832737089, -0.974883471453369, -0.500572917828217, -0.638833484067129,
-0.661876911773614, 0.482613330981802, 0.232976197494883, -0.437203491635383,
-0.375754351084758, -0.421841206497728, -0.88847061755405, 1.20779505745642,
4.21167561465542, 0.750682798752242, 1.07719155496952, 2.10025232445032,
2.1437868252793, 1.42546756160128, 0.990122553311588, -0.270201245687125,
-0.668541928272209, -0.587206054192632, 0.227535614993705, -0.34580111517447,
-1.10019154960626, 1.34403345795275, 1.8268433359891, 1.25350660582093,
1.19315537106641, 0.468940554011867, 0.921574814670944, 3.21492173534361,
0.287886849748252, -0.0742205587790054, -0.315625497797195, 1.58543839697094,
0.378413701880074, -0.858786610588067, -0.526854819438082, 1.10262851893459,
-0.466503584683535, 0.913870401723578, 1.69394221264665, -0.126225346173866,
-0.357357734595538, -1.05075489986049, -0.126225346173866, -0.241791540384716,
0.0760154936950757, -0.299574637490112, 0.0471239451423484, -0.0968113092559779,
0.902046474926439, -1.32907915984528, -1.42862168816586, 0.558796377269263,
-0.405736397147445, 1.27962158234934, -0.203218839529706, -0.347383880545724,
2.03477179719514, 1.34827160188079, 2.03477179719514, 2.03477179719514,
1.00502150422358, 1.86314674836655, 0.0130287219943229, 0.0679487376194715,
1.0393465139893, 0.593121387034955, 0.370008823557797, 1.09287313812892,
2.98712089240292, 0.0437513049925516, 0.364316309562003, 0.0728935781352272,
-0.13110233386351, 0.422600855847354, -0.166073061634724, 0.393458582704678,
0.422600855847354), .Dim = c(30L, 30L), .Dimnames = list(c("2",
"7", "10", "11", "15", "16", "17", "18", "19", "28", "29", "32",
"33", "35", "37", "39", "42", "43", "44", "46", "61", "62", "63",
"64", "66", "67", "68", "69", "71", "72"), c("C0_S", "C2_S",
"C3_S", "C12.DC_S", "C12.1_S", "Ala_S", "Arg_S", "Asn_S", "Cit_S",
"Gln_S", "Gly_S", "His_S", "Ile_S", "Leu_S", "Lys_S", "Orn_S",
"Phe_S", "Pro_S", "Ser_S", "Thr_S", "Trp_S", "Tyr_S", "Val_S",
"Creatinine_S", "Spermidine_S", "t4.OH.Pro_S", "Taurine_S", "total.DMA_S",
"lysoPC.a.C14.0_S", "lysoPC.a.C16.0_S")))
I have tried these to version:
NMDS=metaMDS(plotdf,k=2,trymax=500, distance = "euclidean")
NMDS=metaMDS(daisy(plotdf, metric ="euclidean", stand = F ))
And I get this message:
plot(NMDS)
species scores not available
Is there something I haven't understood for this function, e.g. the negative values when scaling or the choice of distance.. which is causing this problem?
metaMDS finds species scores after the analysis using weighted averages, and these are not well-defined for negative input (as these would imply negative weights). However, if you have used scaled data and Euclidean distances, you can find species scores similarly as in PCA: as fitted vectors. In vegan this would go like this with your example:
NMDS=metaMDS(plotdf,k=2,trymax=500, distance = "euclidean")
envfit(NMDS, plotdf) # species scores

Given an IANA Timezone and a local time, parse it into a Maybe Posix?

Is there a way in Elm to take a local time (such as the string 2019-03-18T09:10:12.4; no offset specifed) and a timezone (such as Australia/Sydney) to a possible Posix value (i.e, that time converted to UTC), without using ports?
There's waratuman/time-extra, but it seems to only work on the Date portion. And sadly rtfeldman/elm-iso8601-date-strings doesn't take timezones.
In JS, there's options such as moment-tz and date-fns-timezone, but it would be much simpler to avoid JS interop for frequent date parsing.
By combining justinmimbs/time-extra and justinmimbs/timezone-data, you should be able to get valid posix entirely in Elm.
Demo: https://ellie-app.com/54tyw9yvsQga1
First, you need to convert your timestampWithoutTimezone to Parts:
toParts : String -> Maybe Parts
toParts timestampWithoutTimezone =
timestampWithoutTimezone
|> Regex.find regex
|> List.map .submatches
|> List.head
|> Maybe.andThen Maybe.Extra.combine
|> Maybe.andThen listToParts
regex : Regex
regex =
Maybe.withDefault Regex.never <|
Regex.fromString "^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})\\.(\\d)$"
monthLookup : Dict String Month
monthLookup =
Dict.fromList [ ( "01", Jan ), ( "02", Feb ), ( "03", Mar ), ( "04", Apr ), ( "05", May ), ( "06", Jun ), ( "07", Jul ), ( "08", Aug ), ( "09", Sep ), ( "10", Oct ), ( "11", Nov ), ( "12", Dec ) ]
listToParts : List String -> Maybe Parts
listToParts list =
let
toInt : Int -> Maybe Int
toInt index =
list |> List.Extra.getAt index |> Maybe.andThen String.toInt
in
Maybe.map2 Parts
(toInt 0)
(list |> List.Extra.getAt 1 |> Maybe.andThen (\month -> Dict.get month monthLookup))
|> Maybe.andThen (\parts -> Maybe.map5 parts (toInt 2) (toInt 3) (toInt 4) (toInt 5) (toInt 6))
Then, using partsToPosix with the appropriate Zone you can get a posix value:
toPosix : Time.Zone -> String -> Maybe Posix
toPosix zone timestampWithoutTimezone =
timestampWithoutTimezone
|> toParts
|> Maybe.map (Time.Extra.partsToPosix zone)
The library author recommends you store evaluated Zone values in your model:
model = { zone = TimeZone.australia__sydney () }
toPosix model.zone "2019-03-18T09:10:12.4"
If we can assume that all of your dates come without a timezone, and that you always want to cast them to Australia/Sydney before converting them to Posix, you should be able to just concatenate the offset yourself to create 2019-03-18T09:10:12.4+11:00:
toPosix : String -> String -> Maybe Posix
toPosix timezone timestampWithoutTimezone =
let
timestampWithTimezone = timestampWithoutTimezone ++ timezone
in
timestampWithTimezone |> toTime |> Result.toMaybe
{- If you always want to use Australia/Sydney -}
localPosix =
toPosix "+11:00"
posix =
localPosix "2019-03-18T09:10:12.4"
{- Custom timezone -}
posix =
toPosix "+11:00" "2019-03-18T09:10:12.4"

Export Plotly Graph in markdown with jupyter notebook

I want to export a notebook with plotly graphs in markdown to put it on my blog, but the Graphs doesn't show when I open the post on my website.
I'm not experienced with javascript, but I can see in the markdown File HTML code related to these graphs, here is an example:
<div id="52c4552b-3ced-4862-859b-5db04f4d3c5a" style="height: 500px; width: 1000px;" class="plotly-graph-div"></div><script type="text/javascript">require(["plotly"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV ||
{};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("52c4552b-3ced-4862-859b-5db04f4d3c5a", [{"type": "area", "r": [6409, 6019, 7254, 7226, 7625, 8353, 7271, 5100, 8178, 8833, 8123, 7091], "t": ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"], "name": "Total Number of accidents", "marker": {"color": "rgb(106,81,163)"}}, {"type": "area", "r": [697, 602, 761, 786, 794, 839, 714, 565, 802, 813, 802, 729], "t": ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"], "name": "Grave accidents", "marker": {"color": "rgb(158,154,200)"}}], {"title": "Repartition of accidents per Hour", "autosize": false, "width": 1000, "height": 500, "orientation": -90}, {"showLink": true, "linkText": "Export to plot.ly"})});</script>
So what should I do for the graphs to appear correctly ?
Thanks !
I think, You have missed is to add the script tag containing plotly.js, You can read more here. So what you need to do, is to include the below line.
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
Also, I assume you are using plotly.offline to make your plots, so you need to convert the plot to html like so.
import pandas as pd
import numpy as np
import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()
N = 10
random_x = np.linspace(0, 1, N)
random_y = np.random.randn(N)
trace = go.Scatter(
x = random_x,
y = random_y
)
data = [trace]
py_offline.plot(data, filename='basic-line', include_plotlyjs=False, output_type='div')
This above topic is discussed in detail -> SO Answer
So with this you will get the plot html as a string.
Note: You always should have only one plotly.js, you can either set include_plotlyjs to true and not include the above script tag, or set it to false and use the script tag, but note, that you need to always have only one plotly.js file present.
So the final content that you need to paste in your blog is going to be.
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="22495cec-ac95-42d5-b3b1-4566a5848585" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("22495cec-ac95-42d5-b3b1-4566a5848585", [{"type": "scatter", "x": [0.0, 0.1111111111111111, 0.2222222222222222, 0.3333333333333333, 0.4444444444444444, 0.5555555555555556, 0.6666666666666666, 0.7777777777777777, 0.8888888888888888, 1.0], "y": [-0.2706323284096669, -0.5368085060076518, -0.3650022122835297, 1.0837185699917664, -1.6123886503326845, 1.3256691068338189, -0.31083903066205104, 0.6951190301897303, -1.624361384686101, 1.852523980751262]}], {}, {"showLink": true, "linkText": "Export to plot.ly"})</script>
Please do let me know if your issue is resolved!

Resources