Plotting in R in Linux Terminal - linux

I have a text file that I converted into a numeric vector:
numbers <- scan("list_of_numbers.txt")
I then put it into a table:
t <- table(numbers)
Which outputs like this:
1 2 3 4 5 6 7 8 9 10 11
621266 496647 436229 394595 353249 305882 253983 199455 147380 102872 67255
12 13 14 15 16 17 18 19 20 21 22
41934 24506 13778 7179 3646 1778 816 436 217 114 74
23 24 25 26 27 28 29 30 31 32 33
49 44 26 21 19 21 20 14 9 17 14
34 35 36 37 38 39 40 41 42 43 44
7 11 9 14 3 5 8 4 4 2 3
45 46 47 55 56 60 62 63 69 70 72
2 1 2 2 2 1 1 1 3 2 1
78 82 85 93 95 114 125 265 331 350
1 1 1 1 1 1 1 1 1 1
How would I plot a line graph with x axis of numbers 1 - 25 and y axis the frequency values of the x axis all in the terminal window?
In addition, how can a plot like this (which is default saved as a .pdf file) be viewd in the linux terminal?
Most commands like less, cat, and xdg-open output a bunch of strange unreadable symbols.

You can use fbi, the linux framebuffer imageviewer to open pdf files in the linux console.
A small problem can be that it needs root privileges. It seems like it can not run through R using system, it complains about not being a linux console. But you can use it in the terminal like:
sudo fbi Rplots.pdf
As for the plotting part of your question you can just use something like:
plot(t, xlim = c(1, 25))
Hope it helps,
alex

I think it's very convenient to use txtplot::txtplot as follow:
> cat("1 2 3 4 5 6", file = "list_of_numbers.txt", sep = "\n")
> numbers <- scan("list_of_numbers.txt")
Read 6 items
> t <- table(numbers)
> txtplot(t)
You can install it just by this command:
install.packages('txtplot')
I found that Jupyter may be the best wheel for us to handle that, and we can equip that following this tutorial: Embed Graphs In Jupyter Notebooks in R
References:
How To Install R on Ubuntu 18.04
scan

Related

Patterns in nested for loops

In python I need the output to be the following, I included the image of the output:
enter image description here
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
I've tried googling the answer but I don't get the exact pattern
Im not sure why you want pattern matching. You can achieve this result with a simple for loop.
for i in range(9):
for j in range(1,i+1):
print((i*j), end=" ")
print("\n")
this should give you the result you want.
_______ = 9
for __, ____ in enumerate(list(range(1,_______+1))):
_____ = f"{____}"
for ___ in range(1,__+1):
_____ = f"{_____} {____*(___+1)}"
print(_____)

printing a string like a matrix

Trying to let the user input a number, and print a table according to the square of its size. Here's an example.
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
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
Size--> 9
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
36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80
Here's is the code that i have tried.
length=int(input('Size--> '))
size=length*length
biglist=[]
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
for i in range(0, len(biglist), length):
print(' '.join(biglist[i: i+length]))
but instead here's what i got
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
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
As you can see the rows are not aligned properly like the example.
What's the simplest way of presenting it in a proper alignment? Thx :)
Using .format on string with right aligning.
And strlen is the number of characters required for each number.
length = int(input('Size--> '))
size = length*length
biglist = []
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
strlen = len(str(length**2-1))+1
for i in range(0, len(biglist), length):
# print(' '.join(biglist[i: i+length]))
for x in biglist[i: i+length]:
print(f"{x:>{strlen}}", end='')
print()

Reading in multidigit command line parameter

I'm learning J and have modified a tutorial into a jconsole script invoked by ./knight.j N to return as output the Knight's tour for an NxN board.
#!/usr/local/bin/j
kmoves=: 3 : 0
t=. (>,{;~i.y) +"1/ _2]\2 1 2 _1 1 2 1 _2 _1 2 _1 _2 _2 1 _2 _1
(*./"1 t e. i.y) <##"1 y#.t
)
ktour=: 3 : 0
M=. >kmoves y
p=. k=. 0
b=. 1 $~ *:y
for. i.<:*:y do.
b=. 0 k}b
p=. p,k=. ((i.<./) +/"1 b{~j{M){j=. ({&b # ]) k{M
end.
assert. ~:p
(,~y)$/:p
)
echo ktour 0".>2}.ARGV
exit''
However, I'm having difficulty in handling ARGV for numbers greater than 9. The script works correctly with single digit input:
$ ./knight.j 8
0 25 14 23 28 49 12 31
15 22 27 50 13 30 63 48
26 1 24 29 62 59 32 11
21 16 51 58 43 56 47 60
2 41 20 55 52 61 10 33
17 38 53 42 57 44 7 46
40 3 36 19 54 5 34 9
37 18 39 4 35 8 45 6
But fails on double digit input:
$ ./knight.j 10
|length error: kmoves
| (*./"1 t e.i.y)<##"1 y #.t
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
It works if I separate the digits of the parameter into different arguments:
$ ./knight.j 1 0
0 17 96 67 14 19 84 35 12 21
99 64 15 18 97 68 13 20 37 34
16 1 98 95 66 85 36 83 22 11
63 92 65 86 81 94 69 72 33 38
2 87 90 93 76 71 82 39 10 23
91 62 53 78 89 80 75 70 73 32
44 3 88 61 52 77 40 59 24 9
47 50 45 54 79 60 27 74 31 58
4 43 48 51 6 41 56 29 8 25
49 46 5 42 55 28 7 26 57 30
ARGV
┌─────────────────┬──────────┬─┬─┐
│/Users/v64/.bin/j│./knight.j│1│0│
└─────────────────┴──────────┴─┴─┘
I understand conceptually why this works, but I can't figure out how to modify the script to accept "10" as a single argument.
Thanks for the additional information on ARGV.I think the issue is that 0 ". > 2}. ARGV is a list of length 1 when '10' is the third box and an atom with shape empty when '9' is in the third box.
ARGV=: '/Users/v64/.bin/j';'./knight.j';'10'
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
$ 0 ".>2}. ARGV NB. 1 item list
1
0 ".>2}. ARGV
10
ARGV=: '/Users/v64/.bin/j';'./knight.j';'9'
$ 0 ".>2}. ARGV NB. atom with empty shape
0 ".>2}. ARGV
9
You can change the shape of the '10' result by using {. on the length 1 list to make it an atom and I think you will find that your verb now works for double digits.
ARGV=: '/Users/v64/.bin/j';'./knight.j';'10'
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
$ {. 0 ".>2}. ARGV NB. Atom with empty shape
{. 0 ".>2}. ARGV
10
I don't imagine this was the reason that you expected, but it does happen from time to time that results that look like atoms are actually 1 item lists which can result in length errors.
Hope this helps.

How to divide 1 column into 5 segments with pandas and python?

I have a list of 1 column and 50 rows.
I want to divide it into 5 segments. And each segment has to become a column of a dataframe. I do not want the NAN to appear (figure2). How can I solve that?
Like this:
df = pd.DataFrame(result_list)
AWA=df[:10]
REM=df[10:20]
S1=df[20:30]
S2=df[30:40]
SWS=df[40:50]
result = pd.concat([AWA, REM, S1, S2, SWS], axis=1)
result
Figure2
You can use numpy's reshape function:
result_list = [i for i in range(50)]
pd.DataFrame(np.reshape(result_list, (10, 5), order='F'))
Out:
0 1 2 3 4
0 0 10 20 30 40
1 1 11 21 31 41
2 2 12 22 32 42
3 3 13 23 33 43
4 4 14 24 34 44
5 5 15 25 35 45
6 6 16 26 36 46
7 7 17 27 37 47
8 8 18 28 38 48
9 9 19 29 39 49

How to calculate 95th percentile in Excel 2010 [duplicate]

This question already has answers here:
Calculate Percentile in Excel 2010
(3 answers)
Closed 9 years ago.
I am trying to calculate how many calls came back in 95 percentile of time. Below is my Result Set. I am working with Excel 2010
Milliseconds Number
0 1702
1 15036
2 14262
3 13190
4 9137
5 5635
6 3742
7 2628
8 1899
9 1298
10 963
11 727
12 503
13 415
14 311
15 235
16 204
17 140
18 109
19 83
20 72
21 55
22 52
23 35
24 33
25 25
26 15
27 18
28 14
29 15
30 13
31 19
32 23
33 19
34 21
35 20
36 25
37 26
38 13
39 12
40 10
41 17
42 6
43 7
44 8
45 4
46 7
47 9
48 11
49 12
50 9
51 9
52 9
53 8
54 10
55 10
56 11
57 3
58 7
59 7
60 2
61 5
62 7
63 5
64 5
65 2
66 3
67 2
68 1
70 1
71 2
72 1
73 4
74 1
75 1
76 1
77 3
80 1
81 1
85 1
87 2
93 1
96 1
100 1
107 1
112 1
116 1
125 1
190 1
356 1
450 1
492 1
497 1
554 1
957 1
Just some background what does above information means-
1702 calls came back in 0 milliseconds
15036 calls came back in 1 milliseconds
14262 calls came back in 2 milliseconds
etc etc
So to calculate the 95th percentile from the above data, I am using this formula in excel 2010-
=PERCENTILE.EXC(IF(TRANSPOSE(ROW(INDIRECT("1:"&MAX(H$2:H$96))))<=H$2:H$96,A$2:A$96),0.95)
Can anyone help me whether the way I am doing in Excel 2010 is right or not?
I am getting 95th percentile as 10 by using the above scenario.
Thanks for the help.
that's essentially the same question you asked here and the formula I suggested. As per my last comments in that question - that formula should work OK as long as you use CTRL+SHIFT+ENTER correctly. I get 10 as the answer for this example using that formula.
I think you can verify manually that that is indeed the correct answer. If you have a running total in an adjacent column then you can see where the 95th percentile is reached......

Resources