how to name matrix columns - dataframe

I have a matrix like below, how can I give the column names like "month", "2015", "2016, "2017" from column 2:5? Thank you.
[,1] [,2] [,3] [,4] [,5]
[1,] "" "1" "75" "75" "94"
[2,] "" "2" "77" "67" "69"
[3,] "" "3" "67" "78" "80"
[4,] "" "4" "71" "99" "84"
[5,] "" "5" "62" "89" "74"

Assuming you're using R, you could do something like this (for matrix M)
colnames(M) <- c("","month","2015","2016","2017")

Related

How to randomly change the picture of several picturebox

I basically want to know how I can optimize this code. I was trying to create somehow a program that could change the photo of a picturebox, but with a randomizer. Basically, each photo would have its percentage of chance to appear in the picturebox, and after several attempts, the only thing that worked for me was this ugly and badly optimized code, but if I try to fix it, the program simply fails. Well, my code generates a random number from 1 to 100 and depending on the number that is generated, one of the six photos is chosen. It is working, however it changes the image from just one picturebox, and I have over 50 that I want to switch (still random). I put it to change everything in a single button, it took me an hour to do this, and I saw that the program gets very slow so it's out of the question. Is there any way to change them all at once using only one code, but keeping random all 50 pictureboxes?
The idea is that at the end of the 50 picturebox are randomly filled with photos, with pictures that can be repeated, but some are less likely to appear than others.
I know, it was a bit confusing, but even if someone can help me, thank you right away.
My code:
randomvalue = random.Next(1, 101)
If randomvalue = "1" Or randomvalue = "2" Or randomvalue = "3" Or randomvalue = "4" Or randomvalue = "5" Or randomvalue = "6" Or randomvalue = "7" Or randomvalue = "8" Or randomvalue = "9" Or randomvalue = "10" Or randomvalue = "11" Or randomvalue = "12" Or randomvalue = "13" Or randomvalue = "14" Or randomvalue = "15" Or randomvalue = "16" Or randomvalue = "17" Or randomvalue = "18" Or randomvalue = "19" Or randomvalue = "20" Or randomvalue = "21" Or randomvalue = "22" Or randomvalue = "23" Or randomvalue = "24" Or randomvalue = "25" Or randomvalue = "26" Or randomvalue = "27" Or randomvalue = "28" Or randomvalue = "29" Or randomvalue = "30" Or randomvalue = "31" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img1.png")
End If
If randomvalue = "32" Or randomvalue = "33" Or randomvalue = "34" Or randomvalue = "35" Or randomvalue = "37" Or randomvalue = "38" Or randomvalue = "39" Or randomvalue = "40" Or randomvalue = "41" Or randomvalue = "42" Or randomvalue = "43" Or randomvalue = "44" Or randomvalue = "45" Or randomvalue = "46" Or randomvalue = "47" Or randomvalue = "48" Or randomvalue = "49" Or randomvalue = "50" Or randomvalue = "51" Or randomvalue = "52" Or randomvalue = "53" Or randomvalue = "54" Or randomvalue = "55" Or randomvalue = "56" Or randomvalue = "57" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img2.png")
End If
If randomvalue = "58" Or randomvalue = "59" Or randomvalue = "60" Or "61" Or randomvalue = "62" Or randomvalue = "63" Or randomvalue = "64" Or randomvalue = "65" Or randomvalue = "66" Or randomvalue = "67" Or randomvalue = "68" Or randomvalue = "69" Or randomvalue = "70" Or randomvalue = "71" Or randomvalue = "72" Or randomvalue = "73" Or randomvalue = "74" Or randomvalue = "75" Or randomvalue = "76" Or randomvalue = "77" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img3.png")
End If
If randomvalue = "78" Or randomvalue = "79" Or "80" Or randomvalue = "81" Or randomvalue = "82" Or randomvalue = "83" Or randomvalue = "84" Or randomvalue = "85" Or randomvalue = "86" Or randomvalue = "87" Or randomvalue = "88" Or randomvalue = "89" Or randomvalue = "90" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img4.png")
End If
If randomvalue = "91" Or randomvalue = "92" Or randomvalue = "93" Or randomvalue = "94" Or randomvalue = "95" Or randomvalue = "96" Or randomvalue = "97" Or randomvalue = "98" Or randomvalue = "99" Or randomvalue = "100" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img5.png")
End If
If randomvalue = "36" Then
btnOre1.Image = Image.FromFile("C:\Users\" & Environment.UserName & "\AppData\Local\imgsx\img6.png")
End If
I don't know what the actual numbers are because I couldn't be bothered counting but lets' say that your six images should have 10%, 20%, 10%, 20%, 10% and 30% chances of being used respectively. You can use a Select Case to easily filter a random number, e.g.
Select Case myRandom.Next(0, 100)
Case Is < 10 '0-9
'Use first image
Case Is < 30 '10-29
'Use second image
Case Is < 40 '30-39
'Use third image
Case Is < 60 '40-59
'Use fourth image
Case Is < 70 '60-69
'Use fifth image
Case Else '70-99
'Use sixth image
End Select
It's also worth noting that the Random class is inheritable so you can actually create your own class that allows you to specify weightings to the outcomes. I have actually created a WeightedRandom class before but seem not to have kept it anywhere. I think that I'll have to do that again and put it somewhere publicly accessible.
EDIT: Here's a more complete example:
Imports System.IO
Public Class Form1
Private folderPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "imgsx")
Private fileNames As String() = {"img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"}
Private imagesByFileName As New Dictionary(Of String, Image)
Private rng As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Create one Image object per file.
For Each fileName In fileNames
Dim filePath = Path.Combine(folderPath, fileName)
Dim img = Image.FromFile(filePath)
imagesByFileName.Add(fileName, img)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Display a random image in each PictureBox on the form.
For Each pictureBox In Controls.OfType(Of PictureBox)
pictureBox.Image = GetWeightedRandomImage()
Next
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
'Clean up the Images to release the files.
For Each fileName In fileNames
imagesByFileName(fileName).Dispose()
Next
End Sub
Private Function GetWeightedRandomImage() As Image
Dim fileNameIndex As Integer
Select Case rng.Next(0, 100)
Case Is < 10 '0-9
fileNameIndex = 0
Case Is < 30 '10-29
fileNameIndex = 1
Case Is < 40 '30-39
fileNameIndex = 2
Case Is < 60 '40-59
fileNameIndex = 3
Case Is < 70 '60-69
fileNameIndex = 4
Case Else '70-99
fileNameIndex = 5
End Select
Return imagesByFileName(fileNames(fileNameIndex))
End Function
End Class

trim trailing tabs from a string/column in awk

I have a file containing 4 columns separated by tabs. In the last column there can be sometimes trailing tabs between the quotation marks.
It is a similar question to trim leading and trailing spaces from a string in awk. Here is an example:
col1 col2 col3 col4
"12" "d" "5" "this is great"
"13" "d" "6" "this is great<tab>"
"14" "d" "7" "this is great<tab><tab>"
"15" "d" "8" "this is great"
"16" "d" "9" "this is great<tab>"
This is what I come up with so far:
gawk --re-interval -F '"' 'NF = 9 {if ($8 ~ /\t$/) {gsub(/[\t]+$,"",$8)} ;}'
The problem is that it destroys my format meaning I get no quotation marks for each column. The good thing is that the tabs in between the columns are still there:
col1 col2 col3 col4
12 d 5 this is great
13 d 6 this is great
14 d 7 this is great
15 d 8 this is great
16 d 9 this is great
What do I do wrong?
You need to tell awk that the output field separator (OFS) is also a quote. For example:
awk -v OFS='"' -F '"' 'NF == 9 {
if ($8 ~ /\t$/) {
gsub(/[\t]+$/,"",$8)
}
}
1' input.txt
Output:
col1 col2 col3 col4
"12" "d" "5" "this is great"
"13" "d" "6" "this is great"
"14" "d" "7" "this is great"
"15" "d" "8" "this is great"
"16" "d" "9" "this is great"

ngrams combination of words hive

I have a table with 1000 rows and 3 variables (an ID, a COUNTRY and a string variable "VAR1"). VAR1 is a sentence composed by words separated by a space.
I want, by COUNTRY, a count of all couples (or all triplets) of words. Very important the couples (or triplets) are a cross-over of all words (not necessarily step by step). Perhaps, we can do it with an ngrams hql-function but when i use it, it's counting words step by step and not all the crossing.
Let's go for the example to give you an idea of what i want :
> **"ID" "COUNTRY" "VAR1"**
> "1" "CANADA" "dad mum child"
> "2" "CANADA" "dad mum dog"
> "3" "USA" "bird lion car"
VAR1 is not necessarily a length of 3 words. It's just to simplify.
The resutls in 4 steps that i want for a 2-ngrams:
STEP 1 : THE MOST IMPORTANT STEP : crossing words by 2
> "1" "CANADA" "dad mum" 1
> "1" "CANADA" "dad child" 1
> "1" "CANADA" "mum dad" 1
> "1" "CANADA" "mum child" 1
> "1" "CANADA" "child dad" 1
> "1" "CANADA" "child mum" 1
> "2" "CANADA" "dad mum" 1
> "2" "CANADA" "dad dog" 1
> "2" "CANADA" "mum dad" 1
> "2" "CANADA" "mum dog" 1
> "2" "CANADA" "dog dad" 1
> "2" "CANADA" "dog mum" 1
> "3" "USA" "bird lion" 1
> "3" "USA" "bird car" 1
> "3" "USA" "lion bird" 1
> "3" "USA" "lion car" 1
> "3" "USA" "car bird" 1
> "3" "USA" "car lion" 1
STEP 2 : order the 2-grams
> "1" "CANADA" "dad mum" 1
> "1" "CANADA" "child dad" 1
> "1" "CANADA" "dad mum" 1
> "1" "CANADA" "child mum" 1
> "1" "CANADA" "child dad" 1
> "1" "CANADA" "child mum" 1
> "2" "CANADA" "dad mum" 1
> "2" "CANADA" "dad dog" 1
> "2" "CANADA" "dad mum" 1
> "2" "CANADA" "dog mum" 1
> "2" "CANADA" "dad dog" 1
> "2" "CANADA" "dog mum" 1
> "3" "USA" "bird lion" 1
> "3" "USA" "bird car" 1
> "3" "USA" "bird lion" 1
> "3" "USA" "car lion" 1
> "3" "USA" "bird car" 1
> "3" "USA" "car lion" 1
STEP 3 : distinct by ID, COUNTRY, 2-ngrams
> "1" "CANADA" "dad mum"
> "1" "CANADA" "child dad"
> "1" "CANADA" "child mum"
> "2" "CANADA" "dad mum"
> "2" "CANADA" "dad dog"
> "2" "CANADA" "dog mum"
> "3" "USA" "bird lion"
> "3" "USA" "bird car"
> "3" "USA" "car lion"
STEP 4 : count by COUNTRY, 2-ngrams
> "CANADA" "dad mum" 2
> "CANADA" "child dad" 1
> "CANADA" "child mum" 1
> "CANADA" "dad dog" 1
> "CANADA" "dog mum" 1
> "USA" "bird lion" 1
> "USA" "bird car" 1
> "USA" "car lion" 1
THANK YOU VERY MUCH
with cte as
(
select t.ID
,t.COUNTRY
,pe.pos
,pe.val
from mytable t
lateral view posexplode (split(VAR1,'\\s+')) pe
)
select t1.COUNTRY
,concat_ws(' ',t1.val,t2.val) as combination
,count (*) as cnt
from cte t1
join cte t2
on t2.id =
t1.id
where t1.pos < t2.pos
group by t1.COUNTRY
,t1.val
,t2.val
;
+----------+--------------+------+
| country | combination | cnt |
+----------+--------------+------+
| CANADA | dad child | 1 |
| CANADA | dad dog | 1 |
| CANADA | dad mum | 2 |
| CANADA | mum child | 1 |
| CANADA | mum dog | 1 |
| USA | bird car | 1 |
| USA | bird lion | 1 |
| USA | lion car | 1 |
+----------+--------------+------+

sscan doesn't returns part of members

I have a set which contains integer values. And I want to retrieve part of it with sscan.
127.0.0.1:6379[1]> smembers d
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
...
But sscan returns full list of members:
127.0.0.1:6379[1]> sscan d 0
1) "0"
2) 1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
....
Is there any way which brings me members page by page(for eg. 10 items for every scan)
Use the COUNT directive as explained in SCAN's documentation to return a fixed number of results.

An SQL statement into SSRS

I tried to incorporate the following into SSRS but failed.
If XXX = “A” then display “AT”
Else if XXX = “B” then display “BEE”
Else if XXX = “C” then display “CAR”
Else display “Other”
I tried
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR", "Other")
You almost had it. For every output in the Switch function must be paired with a condition. Just make your last condition evaluate to True.
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR",
True, "Other"
)
You want something like this:
=iif(Fields!XXX.Value = "A", "AT", iif(Fields!XXX.Value = "B", "BEE", iif(Fields!XXX.Value = "C", "CAR", "Other")))
[check the parens in the expression builder]