Automatically parse selectors with insheetjson in Stata - json

I am trying to build a program that gets data from Statistics Denmark's API using insheetjson in Stata. However, I have not been able to find a solution for the following problem: I want to get the metadata from a random table (in this case: "FOLK1A" which is a table of demographics). This table has variables of region, age, marital status and time. If we take regions as an example, there are 105 regions, so if I run insheetjson using "https://api.statbank.dk/v1/tableinfo/FOLK1A?lang=en", showresponse flatten, I see a very clear pattern:
variables:1:values:1:id = 000
variables:1:values:1:text = All Denmark
variables:1:values:2:id = 084
variables:1:values:2:text = Region Hovedstaden
variables:1:values:3:id = 101
variables:1:values:3:text = Copenhagen
variables:1:values:4:id = 147
variables:1:values:4:text = Frederiksberg
variables:1:values:5:id = 155
variables:1:values:5:text = Dragør
variables:1:values:6:id = 185
variables:1:values:6:text = Tårnby
variables:1:values:7:id = 165
variables:1:values:7:text = Albertslund
variables:1:values:8:id = 151
variables:1:values:8:text = Ballerup
variables:1:values:9:id = 153
variables:1:values:9:text = Brøndby
variables:1:values:10:id = 157
variables:1:values:10:text = Gentofte
...
variables:1:values:100:text = Mariagerfjord
variables:1:values:101:id = 773
variables:1:values:101:text = Morsø
variables:1:values:102:id = 840
variables:1:values:102:text = Rebild
variables:1:values:103:id = 787
variables:1:values:103:text = Thisted
variables:1:values:104:id = 820
variables:1:values:104:text = Vesthimmerlands
variables:1:values:105:id = 851
variables:1:values:105:text = Aalborg
However, I am not able not parse all these regions in a single call. Is there a way to tell insheetjson to get all of these regions, i.e. "variables:1:values:[1-105]:id" in one call? I don't want to run the command several times, thus pinging the server way too much.
Best regards,
Emil Blicher

Related

Retrieve data in sets Pandas

I'm retrieving data from the Open Weather Map API. I have the following code where I'm extracting the current weather from more than 500 cities and I want the log that is giving me separate the data in sets of 50 each
I did a non efficient way that I would really like to improve!
Many many thanks!
x = 1
for index, row in df.iterrows():
base_url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"
query_url = f"{base_url}appid={api_key}&units={units}&q="
city = row['Name'] #this comes from a df
response = requests.get(query_url + city).json()
try:
df.loc[index,"Max Temp"] = response["main"]["temp_max"]
if index < 50:
print(f"Processing Record {index} of Set {x} | {city}")
elif index <100:
x = 2
print(f"Processing Record {index} of Set {x} | {city}")
elif index <150:
x = 3
print(f"Processing Record {index} of Set {x} | {city}")
except (KeyError, IndexError):
pass
print("City not found. Skipping...")

Getting an empty result for newQuery

I've a problem in getting the value of the query $scholars for $lt = $scholars->lat.The result is empty array for dd($lt);
.Any help would be helpful to my school project.
database of Scholar
id lat lng scholar_birthday scholar_GPA
1 10.275667 123.8569163 1995-12-12 89
2 10.2572114 123.839243 2000-05-05 88
3 9.9545909 124.1368558 2002-05-05 89
4 10.1208564 124.8495005 2010-05-05 85
$scholars = (new Scholar)->newQuery()->select('*');
$scholars->whereBetween(DB::raw('TIMESTAMPDIFF(YEAR,scholars.scholar_birthday,CURDATE())'),array($ship_age_from,$ship_age_to));
$scholars->whereBetween(DB::raw('scholar_GPA'),array($ship_gpa_from,$ship_gpa_to));
$lt = $scholars->lat;
$lg = $scholars->lng;
$str = $lt.','.$lg;
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lt).','.trim($lg).'&sensor=false';
$json = #file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
$data->results[0]->formatted_address;
dd($lt);
$scholars = $scholars->get();
dd Result
Undefined property: Illuminate\Database\Eloquent\Builder::$lat
Two things,
when you use the newQuery() you will still need to get() the result like such
$scholars = (new Scholar)->newQuery()->select('*')->get();
This however will retrieve a collection and not a single result so you will need to loop over this.
foreach($scholars as $scholar){
$lt = $scholars->lat;
dd($lt);
}

What does this recursive function mean?

I've been tasked with implementing a recursive function in MIPS. This function is
function1(n) = n-5 (if n <= 3)
otherwise = 4*function1(n-1) - n*function1(n-3)
One of the test cases is if n = 6, then the result is 200.
How do you get 200 from entering 6 in this function? To me it looks like the answer should be 2. Is there something I am not understanding about recursion, or am I understanding the function wrong? I am so confused
You seem to be misunderstanding the function somewhere. Here are the steps I followed to arrive at 200:
function1(6) = 4*function1(5) - 6*function1(3) (by rule 2)
function1(5) = 4*function1(4) - 5*function1(2) (by rule 2)
function1(4) = 4*function1(3) - 4*function1(1) (by rule 2)
function1(3) = 3-5 = -2 (by rule 1)
function1(2) = 2-5 = -3 (by rule 1)
function1(1) = 1-5 = -4 (by rule 1)
Substituting back...
function1(4) = 4*-2 - 4*-4 = -8 - -16 = 8
function1(5) = 4*8 - 5*-3 = 32 - -15 = 47
function1(6) = 4*47 - 6*-2 = 188 - -12 = 200

Read in search strings from text file, search for string in second text file and output to CSV

I have a text file named file1.txt that is formatted like this:
001 , ID , 20000
002 , Name , Brandon
003 , Phone_Number , 616-234-1999
004 , SSNumber , 234-23-234
005 , Model , Toyota
007 , Engine ,V8
008 , GPS , OFF
and I have file2.txt formatted like this:
#==============================================
# 005 : Model
#------------------------------------------------------------------------------
[Model] = Honda
option = 0
length = 232
time = 1000
hp = 75.0
k1 = 0.3
k2 = 0.0
k1 = 0.3
k2 = 0.0
#------------------------------------------------------------------------------
[Model] = Toyota
option = 1
length = 223
time = 5000
speed = 50
CCNA = 1
#--------------------------------------------------------------------------
[Model] = Miata
option = 2
CCNA = 1
#==============================================
# 007 : Engine
#------------------------------------------------------------------------------
[Engine_Type] = V8 #1200HP
option = 0
p = 12.0
pp = 12.0
map = 0.4914
k1mat = 100
k2mat = 600
value =12.00
mep = 79.0
cylinders = 8
#------------------------------------------------------------------------------
[Engine_Type] = v6 #800HP
option = 1
active = 1
cylinders = 6
lim = 500
lim = 340
rpm = 330
start = 350
ul = 190.0
ll = 180.0
ul = 185.0
#==============================================
# 008 : GPS
#------------------------------------------------------------------------------
[GPS] = ON
monitor = 0
#------------------------------------------------------------------------------
[GPS] = OFF
monitor = 1
Enable = 1
#------------------------------------------------------------------------------
[GPS] = Only
monitor = 2
Enable = 1
#==============================================
# 014 :Option
#------------------------------------------------------------------------------
[Option] = Disable
monitor = 0
#------------------------------------------------------------------------------
[Option] = Enable
monitor = 1
#==============================================
# 015 : Weight
#------------------------------------------------------------------------------
[lbs] = &1
weight = &1
#==============================================
The expected output is supposed to look like this:
Since there is only option 005-008 in file1.txt the output would be:
Code:
#==============================================
# 005 : Model
#------------------------------------------------------------------------------
[Model] = Toyota
option = 1
length = 223
time = 5000
speed = 50
CCNA = 1
#==============================================
# 007 : Engine
#------------------------------------------------------------------------------
[Engine_Type] = V8 #1200HP
option = 0
p = 12.0
pp = 12.0
map = 0.4914
k1mat = 100
k2mat = 600
value =12.00
mep = 79.0
cylinders = 8
#==============================================
# 008 : GPS
#------------------------------------------------------------------------------
[GPS] = OFF
monitor = 1
Enable = 1
#-----------------------------------------------------------------
Now, using Awk and the values from the 2nd and 3rd columns in file1, I want to search for those strings in file2 and output everything in that section to a CSV file ie from where the string is found to where there is the #-------------
demarcation.
Could someone please help me with this and explain also? I am new to Awk
Thank you!
I wouldn't really use awk for this job as specified, but here's a little snippet to get started:
awk -F'[ ,]+' 'FNR == NR { section["[" $2 "]"] = $3; next }
/^\[/ && section[$1] == $3, /^#/' file1.txt file2.txt
1) The -F'[ ,]+' sets the field separator to one or more of spaces and/or commas (since file1.txt looks like it's not a proper CSV file).
2) FNR == NR (record number in file equals total record number) is only true when reading file1.txt. So for each line in file1.txt, we record [second_field] as the pattern to look for with the third field as value.
3) Then we look for lines that begin with a [ and where the value stored in section for the first field of that line matches the third field of that line (/^\[/ && section[$1] == $3), and print from that line until the next line that begins with a #.
The output for your example input is:
[Model] = Toyota
option = 1
length = 223
time = 5000
speed = 50
CCNA = 1
#--------------------------------------------------------------------------
[GPS] = OFF
monitor = 1
Enable = 1
#------------------------------------------------------------------------------
The matched lines in step 3 were [Model] = Toyota and [GPS] = OFF. The Engine line is missing because file2.txt had Engine_Type instead. Also, I didn't bother with the section headers; it would be easy to add another condition to print them all but it requires lookahead to print only the ones that are going to have matching content in them (because at the time you read the header you don't know if a match is found inside). For that, I would switch to another language (e.g., Ruby).

HowTo: select all rows in a cell array, where a particular column has a particular value

I have a cell array, A. I would like to select all rows where the first column (for example) has the value 1234 (for example).
When A is not a cell array, I can accomplish this by:
B = A(A(:,1) == 1234,:);
But when A is a cell array, I get this error message:
error: binary operator `==' not implemented for `cell' by `scalar' operations
Does anyone know how to accomplish this, for a cell array?
The problem is the expression a(:,1) == 1234 (and also a{:,1} == 1234).
For example:
octave-3.4.0:48> a
a =
{
[1,1] = 10
[2,1] = 13
[3,1] = 15
[4,1] = 13
[1,2] = foo
[2,2] = 19
[3,2] = bar
[4,2] = 999
}
octave-3.4.0:49> a(:,1) == 13
error: binary operator `==' not implemented for `cell' by `scalar' operations
octave-3.4.0:49> a{:,1} == 13
error: binary operator `==' not implemented for `cs-list' by `scalar' operations
I don't know if this is the simplest or most efficient way to do it, but this works:
octave-3.4.0:49> cellfun(#(x) isequal(x, 13), a(:,1))
ans =
0
1
0
1
octave-3.4.0:50> a(cellfun(#(x) isequal(x, 13), a(:,1)), :)
ans =
{
[1,1] = 13
[2,1] = 13
[1,2] = 19
[2,2] = 999
}
I guess the Class of A is cell. (You can see in the Workspace box).
So you may need to convert A to the matrix by cell2mat(A).
Then, just like Matlab as you did: B = A(A(:,1) == 1234,:);
I don't have Octave available at the moment to try it out, but I believe that the following would do it:
B = A(A{:,1} == 1234,:);
When dealing with cells () returns the cell, {} returns the contents of the cell.