Sort by two or more variables - stata

I'm trying to sort by ID and then by Date.
What I have:
| ID | Date |
| ----------------------|
| 112 | 2013-01-01 |
| 112 | 2013-01-15 |
| 113 | 2012-01-01 |
| 112 | 2014-02-13 |
| 112 | 2013-01-02 |
| 113 | 2011-01-11 |
What I need:
| ID | Date |
| ----------------------|
| 112 | 2013-01-01 |
| 112 | 2013-01-02 |
| 112 | 2013-01-15 |
| 112 | 2014-02-13 |
| 113 | 2011-01-11 |
| 113 | 2012-01-01 |
My problem is that I only know how to sort by ID or Date.

More generally:
clear
input id foo
1 56
1 34
2 13
1 67
1 22
2 89
2 61
2 76
end
sort id (foo)
list, sepby(id)
+----------+
| id foo |
|----------|
1. | 1 22 |
2. | 1 34 |
3. | 1 56 |
4. | 1 67 |
|----------|
5. | 2 13 |
6. | 2 61 |
7. | 2 76 |
8. | 2 89 |
+----------+
In a more advanced programming context you can use the same syntax with bysort.

Related

Why do some pins don't work with wiringPi?

I'm trying to write a simple program which uses the GPIO pins of the Raspberry Pi 3B.
When I run the following program, only the LED on pin 17 flashes.
Pin 27 doesn't blink and doesn't get set to OUT.
I don't get any error messages.
int main(){
if (wiringPiSetupSys() == -1){
std::cout << "wiringpsetup failed\n";
exit(1);
}
auto pin = 17;
auto pin1 = 27;
pinMode(pin, OUTPUT);
pinMode(pin1, OUTPUT);
for (auto i = 0; i < 8; ++i)
{
digitalWrite(pin, HIGH);
digitalWrite(pin1, HIGH);
delay(500);
std::cout << "high\n";
digitalWrite(pin, LOW);
digitalWrite(pin1, LOW);
delay(500);
std::cout << "low\n";
}
}
However, I can make the LED on pin 27 blink by executing the following commands in the terminal:
gpio -g mode 27 out
gpio -g write 27 0
gpio -g write 27 1
gpio -g write 27 0
Consequently, the LED is properly connected and not broken.
A little additional information:
pi#raspberrypi:~$ gpio -g readall
+-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 0 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | OUT | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | OUT | 1 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | OUT | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 1 | OUT | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
I still don't know why it doesn't work in the example, but I found another solution:
Instead of wiringPiSetupSys() I use now the function wiringPiSetupPhys() and accordingly the correct pins (13 and 15).
If someone knows a solution with wiringPiSetupSys(), I would still be glad about an answer...

How to Compare Averages of Categories

I have 3 tables:
table1:
Category
--------
1
2
3
4
table2:
Branch | Category
------------------
A1 | 1
A2 | 1
A3 | 2
A4 | 3
A5 | 4
A6 | 4
A7 | 4
table3: Values
Date | Branch | Value
---------------------------
1/1/2019 | A1 | 35
1/1/2019 | A2 | 40
1/1/2019 | A4 | 62
1/1/2019 | A5 | 10
1/1/2019 | A6 | 20
1/1/2019 | A7 | 35
Table Connections are:
Category -->--- Branch
and
Branch -->-- Values
I am trying to compare the value of every Branch with the average of the Category that belongs to. If the value of the Branch is below Category's average then it is good else is bad.
If I create a matrix with Branch, value and Category Average
it looks like this :
Branch | Value | Category Average
-------------------------------------
A1 | 35 | 35
A2 | 40 | 40
A4 | 62 | 62
A5 | 10 | 10
A6 | 20 | 20
A7 | 35 | 35
BranchAVG = AVERAGE(Table3[Value])
CategoryAVG = AVERAGEX(VALUES(Categories[Category]);[BranchAVG])
But it should look like this:
Branch | Value | Category Average | Good/Bad
------------------------------------------------
A1 | 35 | 37.5 | Good
A2 | 40 | 37.5 | Bad
A4 | 62 | 62.0 | Good
A5 | 10 | 21.7 | Good
A6 | 20 | 21.7 | Good
A7 | 35 | 21.7 | Bad
Is there any possible way to do that?
Thanks in advance
Elias
You can do this by adding a Category column to your Value table:
Category = RELATED(Branch[Category])
And after this add the AgeragePerCategory column:
AgeravePerCategory = CALCULATE(AVERAGE('Values'[Value]);FILTER('Values'; 'Values'[Category] = EARLIER('Values'[Category])))
Thanks for the answer.
To work it out I had to add an additional filter to Date, because it averages all values regardless the date.
So the AveragePerCategory it looks like:
AveragePerCategory = CALCULATE(AVERAGE('Values'[Value]);FILTER('Values'; 'Values'[Category] = EARLIER('Values'[Category]) && 'Values'[Date] = EARLIER('Values'[Date])))

After appending, I get null values in primary table headers

I have a table that I want to use as headers for another table that just has data. I used append as new in PBI, used the headers table as primary and data table as secondary. All the columns from the primary table have null values and the data table is appended next to headers column.
Eg:
Table 1 ( Headers)
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
Table 2 ( Data )
+----+----+----+----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+----+----+----+----+
Table I am getting after append:
+------+------+------+------+------+------+------+------+
| ABC | DEF | IGH | KLM | null | null | null | null |
+------+------+------+------+------+------+------+------+
| null | null | null | null | 1 | 2 | 3 | 4 |
| null | null | null | null | 6 | 7 | 8 | 9 |
| null | null | null | null | 11 | 12 | 13 | 14 |
| null | null | null | null | 16 | 17 | 18 | 19 |
| null | null | null | null | 21 | 22 | 23 | 24 |
| null | null | null | null | 26 | 27 | 28 | 29 |
| null | null | null | null | 31 | 32 | 33 | 34 |
+------+------+------+------+------+------+------+------+
Table I need:
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
I used Append as new in PBI
Used the headers table ( Table 1) as primary and appended Table 2 to it.
This shows at the top function:
= Table.Combine({Table 1, Table 2})
This in the advanced editor:
let
Source = Table.Combine({Sheet1, InterviewQn})
in
Source
Expected result:
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
OR
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
If you're only trying to rename the columns of Table 2, using the column names of Table 1, then it's simply:
= Table.RenameColumns(#"Table 2", List.Zip({Table.ColumnNames(#"Table 2"), Table.ColumnNames(#"Table 1")}))
See https://pwrbi.com/so_55529969/ for worked example PBIX file

Power BI DAX to filter common items A & B share

Sample data:
| Vendor | Size Group | Model | Quantity | Cost | TAT | Posting Date |
|--------|------------|-------|----------|-------|-----|-------------------|
| A | S | A150 | 150 | 450 | 67 | July 7, 2018 |
| A | M | A200 | 250 | 1500 | 75 | June 22, 2018 |
| A | M | A150 | 25 | 8500 | 85 | July 9, 2018 |
| C | L | A200 | 350 | 1250 | 125 | March 5, 2018 |
| C | XL | A500 | 150 | 6500 | 45 | February 20, 2018 |
| A | M | A900 | 385 | 475 | 40 | January 29, 2018 |
| A | M | A150 | 650 | 45 | 45 | August 31, 2018 |
| D | M | A150 | 65 | 7500 | 15 | April 10, 2018 |
| D | M | A300 | 140 | 3420 | 10 | April 3, 2018 |
| E | S | A150 | 20 | 10525 | 85 | January 3, 2018 |
| B | S | A150 | 30 | 10500 | 40 | June 3, 2018 |
| B | S | A150 | 450 | 450 | 64 | April 3, 2018 |
| E | XS | A900 | 45 | 75 | 60 | January 3, 2018 |
| F | M | A900 | 95 | 655 | 175 | January 3, 2018 |
| D | XL | A300 | 15 | 21500 | 25 | January 3, 2018 |
| D | S | A500 | 450 | 65 | 25 | May 3, 2018 |
| A | M | A350 | 250 | 450 | 22 | January 3, 2018 |
| B | S | A150 | 45 | 8500 | 28 | January 3, 2018 |
| A | S | A300 | 550 | 650 | 128 | January 3, 2018 |
| C | M | A150 | 1500 | 855 | 190 | January 3, 2018 |
| B | M | A150 | 65 | 1750 | 41 | January 3, 2018 |
| A | L | A500 | 75 | 1700 | 24 | January 3, 2018 |
| B | S | A900 | 55 | 9800 | 37 | May 29, 2018 |
| B | M | A500 | 150 | 850 | 83 | April 18, 2018 |
In the provided sample, the common Size Groups A & B both share are S & M. So, I was hoping to display those Size Groups as the legend and Average Cost as the value in a clustered column chart.
Can anyone please advise how I can go about this?
Thank you!!!

Editing a text file in python?

I have this in a text file :
Rubble HM3 80 HM2 90 HM4 92
Bunny HM2 92 HM5 70 HM1 98
Duck HM1 86 HM3 100 HM2 93 HM4 94
Chipmunk HM4 96 HM1 86
Simpson HM3 70 HM1 90 Test1 90
and i want to write a code that changes it to this :
Name | HM1 | HM2 | HM3 | HM4 | Avg. |
________________________________________________
Bunny | 98 | 92 | 0 | 0 | 47.50 |
Chipmunk | 86 | 0 | 0 | 96 | 45.50 |
Duck | 86 | 93 | 100 | 94 | 93.25 |
Rubble | 0 | 90 | 80 | 92 | 65.50 |
Simpson | 90 | 0 | 70 | 0 | 40.00 |
so far :
my_file=open("C:/python27/tools/student_grades.txt", "r+")
my_file_pointer=my_file.read()
for lines in my_file_pointer:
x=my_file_pointer.replace("HM2","|")
print x
Go Easy first time programmer . :)
and if i use the replace function how can i print it all at once and then sort it under every subject "HM1" ?