Use int64 nanotimes as array dimnames in R
I have some lovely nanotimes:
> mynames
[1] "20200415T00:29:00.000000000+00:00" "20200415T00:33:00.000000000+00:00"
Which I'd like to add as dimnames to my array:
> a
[,1] [,2]
[1,] NA NA
[2,] NA NA
However, they seem to be converted into rolled over strings:
> dimnames(a)[1] < list(values=mynames)
> a
[,1] [,2]
1.39309697650764e202 NA NA
1.39315136717363e202 NA NA
> dimnames(a)
[[1]]
[1] "1.39309697650764e202" "1.39315136717363e202"
How can I keep my dimname assignment as nanotime/integer64s? Or if they are forced to character to be dimnames, how can I at least keep them from being strings of incorrect numbers?
You can see for example that these fail to roundtrip back to suitable times:
> nanotime(dimnames(a)[1][[1]][1])
Error in RcppCCTZ::parseDouble(x, fmt = format, tz = tz) :
Parse error on 1.39309697650764e202
1 answer

The documentation of
dimnames
states that thevalue
is coerced tocharacter
. To do all the coercing back and from tonanotime
which is based oninteger64
, you can first coerce yournanotime
tointeger64
and then tocharacter
. Then the back coercion works:library(nanotime) mynames < c(nanotime("20200415T00:29:00.000000000+00:00"), nanotime("20200415T00:33:00.000000000+00:00")) a < array(NA, dim = c(2, 2)) # nanotime > integer64 > charachter dimnames(a)[1] < list(values = as.character(as.integer64(mynames))) # character > integer64 > nanotime nanotime(as.integer64(dimnames(a)[1][[1]][1])) [1] "20200415T00:29:00.000000000+00:00"
See also questions close to this topic

R pass a vector in crossing function
I'm trying to pass a vector in the tidyr 'crossing' function, but I don't see how to do it. Here's what I want to do: I want to crossproduct the objects michel, anita, robert to obtain the dataframe everybody as follow:
# objects to combine michel < c("Berlin","Tokyo","Amsterdam") anita < c("London","Montreal") robert < "Moscow" # combination library(tidyr) everybody < crossing(michel,anita,robert)
Thing is, I want to pick the list michel, anita, robert from a dataframe (or vector), then pass that vector to the crossing function (so that everybody2 is the same as everybody):
# vector with names: vector1 < as.data.frame(c("michel","anita","robert")) # how to pass 'vector1' into crossing? everybody2 < crossing(vector1)
I don't see how I can make it work... any advice?
Thank you in advance!

rStudio Does Not Show Date on Chart Correctly
Here is my script so you can copy paste.
(Dataset contains .csv file containing Dates + Daily Closing Prices of Nasdaq 100)
The script works beautifully except it refuses to show dates on x axis (see screenshot). Please help, here is the script I've been working on:
library(tidyverse)
library(tseries)
library(forecast)
nasdaq < read_csv("Desktop/r data/^NDX.csv")
View(nasdaq)
as.Date(df$Date, "%d%m%y")
pClose < ts(nasdaq[, 'Close'], start =c(2020,5), frequency = 52)
fit_ARIMA < auto.arima(pClose, seasonal = TRUE)
print(summary(fit_ARIMA))
fcast < forecast(fit_ARIMA, h = 5)
autoplot
plot(fcast)
print(summary(fcast))

How do I fix font error when using ggcorrplot?
I tried to reproduce this code:
# Mendeteksi peubah  peubah yang saling berkorelasi secara kuat korelasi < cor(df) # menghitung koefisien korelasi ggcorrplot(korelasi)
Yet I got this result:
And I tried to install mscorefonts on my OS, still got this font error. Does anybody know how to fix this error? Thanks.

How to create a dinamic range for x axis using Google Charts 099,100199... depending on the max value?
I need to build a graph using Google Charts based on the information that I have in my array. The issue is than i do not understand how to start making it because I do not have any idea how to build the xaxis.
My array:
0: Shopping {name: "pedro", desc: "", amount: 1250, …} 1: Shopping {name: "marie", desc: "", amount: 250, …} 2: Shopping {name: "fia", desc: "", amount: 270, …} ...
In the xaxis, is going to have the range of the amounts that starts from 099, 100199.. to the max value that exist in my whole array called Shopping. For example like in the case that I have in my array, now the max value seems to be 1250, so the range in the xaxis have to go from 099, 100199, 200299... to 12001299 that is my latest range.
In the yaxis, I have the quantity of how many values from the amount are in that range, for example the array shopping key one and two, are in the range in 200299, so the bar is going to go to the number 2, and the array shopping key cero is going to go to the number 1 because only exist an amount that belongs in the range 12001299
Does anyone know how to do it? I was thinking on counting the length of the amount and then minus 1. So I will have two numbers in the case that the amount would be 438, and then i will see which is the first number and try to loop and create arrays Rows. I did not try to run this idea because i still do not understand how to create arrays for data.addRows for Google Charts.
I can not use Jquery. If anyone can explain me how it would be the code or which is the solution for me, i will be totally greatful. Thanks.

how to overcome problem in integer overflow even using long also wrong answers?
for input 2:100000,90000 getting wrong answer even i used long
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int a = s.nextInt(); int arr[] = new int[a]; for(int i=0;i<a;i++) arr[i] = s.nextInt(); Arrays.sort(arr); long sum=0; sum=(arr[a1])*(arr[a2]); System.out.println(sum); } }
how to overcome problem in integer overflow even using long also wrong answers ?

optimization problems in c++
I have created a program that gives me the least consecutive value of a fix, but it is very inefficient.
example:
7 4 // Array size and consecutive values
6 4 2 7 10 5 1 //Arrangement
1 3 4 2 // Consecutive values
the smallest consecutive values in order are: 1,12,19,6
How can it be improved?
#include<iostream> #include<vector> using namespace std; vector<int>recorrido, base; int suma_elementos(int elemento); int consecutivo; int main() { int a, b, valor, final = 9999999, total = 0; cin >> a >> b; for (int i = 0; i < a; i++) { cin >> valor; recorrido.push_back(valor); } for (int i = 0; i < b; i++) { cin >> valor; base.push_back(valor); } for (int i = 0; i < b; i++) { suma_elementos(base[i]); for (int j = 0; j < consecutivo; j++) { for (int c = 0; c < base[i]; c++) { total += recorrido[c + (j)]; } if (total < final) { final = total; } total = 0; } cout << final << " "; //reset consecutivo = 0; final = 99999999; total = 0; } return 0; } //Suma de elementos int suma_elementos(int elemento) { int proporcion = recorrido.size(); while (proporcion >= elemento) { proporcion; consecutivo++; } //cout << consecutivo << " "; return 0; }

How to compute the total execution time in multithread application
There is an application that I need to test to see the relationship between number of threads and the total duration of an execution in an multithreaded application. That is what I am trying at the moment:
//Start time of thread executions. long start = System.currentTimeMillis(); //Here, I am initializing the thread list which will store each thread. MyThreadClass[] threads = new MyThreadClass[numOfThreads]; //Start each thread. for (int i = 0; i < threads.length; i++) { threads[i] = new MyThreadClass(/*Constructor parameters here*/); //Start the thread. threads[i].start(); //There is a variable assignment to change the parameter of a constructor on next iteration. } //Join all the threads. for (MyThreadClass thread : threads) { thread.join(); } //After all threads are done, compute the elapsed time to get computation time. long finish = System.currentTimeMillis(); long duration = finish  start; System.out.println("Total duration of the computation with " + threads.length + " thread is " + duration + " milliseconds.");
But here is the problem when I have tested the application by wrapping the code inside the
main()
with some for loop to test. I believe, something is irrational with those number.Total duration of the computation with 1 thread is 50 milliseconds. Total duration of the computation with 2 thread is 25 milliseconds. Total duration of the computation with 3 thread is 25 milliseconds. Total duration of the computation with 4 thread is 15 milliseconds. Total duration of the computation with 5 thread is 19 milliseconds. Total duration of the computation with 6 thread is 16 milliseconds. Total duration of the computation with 7 thread is 15 milliseconds. Total duration of the computation with 8 thread is 25 milliseconds. Total duration of the computation with 9 thread is 20 milliseconds. Total duration of the computation with 10 thread is 20 milliseconds. Total duration of the computation with 11 thread is 30 milliseconds. Total duration of the computation with 12 thread is 34 milliseconds. Total duration of the computation with 13 thread is 34 milliseconds. Total duration of the computation with 14 thread is 18 milliseconds. Total duration of the computation with 15 thread is 18 milliseconds. Total duration of the computation with 16 thread is 18 milliseconds. Total duration of the computation with 17 thread is 15 milliseconds. Total duration of the computation with 18 thread is 19 milliseconds. Total duration of the computation with 19 thread is 17 milliseconds. Total duration of the computation with 20 thread is 13 milliseconds. Total duration of the computation with 21 thread is 13 milliseconds. Total duration of the computation with 22 thread is 13 milliseconds. Total duration of the computation with 23 thread is 12 milliseconds. Total duration of the computation with 24 thread is 12 milliseconds. Total duration of the computation with 25 thread is 14 milliseconds. Total duration of the computation with 26 thread is 15 milliseconds. Total duration of the computation with 27 thread is 18 milliseconds. Total duration of the computation with 28 thread is 20 milliseconds. Total duration of the computation with 29 thread is 20 milliseconds. Total duration of the computation with 30 thread is 15 milliseconds. Total duration of the computation with 31 thread is 22 milliseconds. Total duration of the computation with 32 thread is 23 milliseconds. Total duration of the computation with 33 thread is 17 milliseconds. Total duration of the computation with 34 thread is 33 milliseconds. Total duration of the computation with 35 thread is 20 milliseconds. Total duration of the computation with 36 thread is 27 milliseconds. Total duration of the computation with 37 thread is 20 milliseconds. Total duration of the computation with 38 thread is 23 milliseconds. Total duration of the computation with 39 thread is 26 milliseconds. Total duration of the computation with 40 thread is 20 milliseconds. Total duration of the computation with 41 thread is 30 milliseconds. Total duration of the computation with 42 thread is 22 milliseconds. Total duration of the computation with 43 thread is 20 milliseconds. Total duration of the computation with 44 thread is 20 milliseconds. Total duration of the computation with 45 thread is 20 milliseconds. Total duration of the computation with 46 thread is 21 milliseconds. Total duration of the computation with 47 thread is 26 milliseconds. Total duration of the computation with 48 thread is 22 milliseconds. Total duration of the computation with 49 thread is 22 milliseconds. Total duration of the computation with 50 thread is 16 milliseconds. Total duration of the computation with 51 thread is 14 milliseconds. Total duration of the computation with 52 thread is 18 milliseconds. Total duration of the computation with 53 thread is 12 milliseconds. Total duration of the computation with 54 thread is 20 milliseconds. Total duration of the computation with 55 thread is 21 milliseconds. Total duration of the computation with 56 thread is 30 milliseconds. Total duration of the computation with 57 thread is 30 milliseconds. Total duration of the computation with 58 thread is 30 milliseconds. Total duration of the computation with 59 thread is 25 milliseconds. Total duration of the computation with 60 thread is 15 milliseconds. Total duration of the computation with 61 thread is 10 milliseconds. Total duration of the computation with 62 thread is 18 milliseconds. Total duration of the computation with 63 thread is 12 milliseconds. Total duration of the computation with 64 thread is 15 milliseconds. Process finished with exit code 0
For instance, time that is required to perform the task with 7 threads is the same with 64 threads. There is something wrong in there, but I could not manage to fix it. I have tested the program without an iteration already, that is by providing some inputs for the total number of threads manually and nothing has changed.
run()
function performs afor
iteration only andMyThreadClass
extendsThread
class of Java. I suppose, I need a some sort of mechanism to kill the threads after they have completed, but I suppose joining them already does that. Please point out anything that seems wrong to you. Thanks in advance.Edit: the loop inside the
run()
iterates a million times. That is the fixed variable of the experiment you might say. 
Java nanoTime not throwing reliable results
I am doing a project where I need to implement bubblesort, shellsort and quicksort algorithms in an array of 999 random doubles.
I need to measure the time it takes every algorithm to run.
I am using System.nanoTime() to measure this execution time for each algorithm. I have 3 buttons, one for each algorithm. Upon clicking a button, the timer starts, the function is called, the end timer is then called and the duration is calculated by having endtimestarttime. The duration is then printed on a label on top of the button.
public double[] randomArray = SortAlg.getArray(); private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { long startTimeBubble = System.nanoTime(); SortAlg.bubble(randomArray); long endTimeBubble = System.nanoTime(); long durationBubble = (endTimeBubble  startTimeBubble) / 1000000; bubbleTiempo.setText("bubble took " + durationBubble + " ms"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { long startTimeShellSort = System.nanoTime(); SortAlg.shellSort(randomArray); long endTimeShellSort = System.nanoTime(); long durationShellSort = (endTimeShellSort  startTimeShellSort) / 1000000; shellSortTime.setText("ShellSort took " + durationShellSort + " ms"); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { long startTimeQuickSort = System.nanoTime(); SortAlg.quickSort(randomArray, 0, randomArray.length  1); long endTimeQuickSort = System.nanoTime(); long durationQuickSort = (endTimeQuickSort  startTimeQuickSort) / 1000000; quickSortTime.setText("Quicksort took " + durationQuickSort + " ms"); }
General clarifications: 1getArray() generates a random array of 999 real (double) numbers between 0 and 2000.
2None of the algorithm methods return an array, so this randomArray is NOT being saved as sorted, therefore, every time the button is clicked, the random array that is passed into the function is unsorted.
3Every button has a unique label to display the results.
4What you're seeing there is from the GUI class I created with Netbeans GUI editor, the buttons portion. Main simply creates a new GUI frame and sets it as visible, and the SortAlg contains the logic for the algorithms, which I already tested to be working.
But I have 2 issues here: If I click the button more than 1 time, the time decreases further for every click, until it's eventually 0, which makes no sense.
Also, I initially had a calculation error and I was dividing the time taken by 100000 rather than 1000000, but oddly enough, this was never giving me any errors. When I divide it by 1000000 though (which should be the correct operation for converting ns to ms) I sometimes get 0 MS execution time, which again makes no sense.
Lastly, and I know that this might be difficult to determine, but I was under the impression that quickSort had to be the fastest algorithm? But their execution times greatly vary within builds. Sometimes each one is 5 ms, other times its 30, other times its 68. I am concerned because it seems that, the first clicked is always the fastest one and the other ones end up being slower. I am not confident about these results at all.