I am working on a project in PowerBI.
I want to convert a column of decimals (percentages) to a range from -1 to 1.
This is the table I'm using right now:
FirstName | Score
Jack | 0.75
John | 0.50
Reese | 0.00
Mike | 1.00
And the desired result is:
FirstName | Score
Jack | 0.50
John | 0.00
Reese | -1.00
Mike | 1.00
I am strugling with the math behind this. If I got that part, I could make it by using m-language in PowerBI.
Any ideas, suggestions?
Those scores don't actually look like percentages, they're more like fractions - for example, the percentage for 0.75 would be 75.
So, if you're talking about mapping 0..1 to -1..1, it's a simple matter to apply the transformation:
newVal = oldVal * 2 - 1
The multiplication by two first scales it into the range 0..2 while the subtraction then shifts it into the range -1..1.
I think it's very entitled of you to present no work at all and ask for a free solution. Any basic statistics text will describe how to rebase and scale a set of data. However, this will do what you ask
use strict;
use warnings 'all';
use List::Util qw/ min max /;
my #data = <DATA>;
my ($min, $max, $delta);
{
my #n = map /([\d.]+)\s*\z/, #data;
( $min, $max ) = ( min(#n), max(#n) );
$delta = $max - $min;
}
for ( #data ) {
s{ ( [\d.]+ ) (?= \s*\z ) }{ sprintf '%.2f', ( $1 - $min ) * 2 / $delta - 1 }xe;
print;
}
__DATA__
FirstName | Score
Jack | 0.75
John | 0.50
Reese | 0.00
Mike | 1.00
output
FirstName | Score
Jack | 0.50
John | 0.00
Reese | -1.00
Mike | 1.00
Related
I have a dataframe col_metadata in R that goes as:
sample | b | c | ...
____________________
S1 | 1 | 1 | ...
S2 | 1 | 2 | ...
S3 | 2 | 2 | ...
S4 | 3 | 3 | ...
I want to make a function that gives me samples that have given values in front of them. For eg.,
fun(b,c(1,2))
should return
S1 S2 S3
while
fun(c,c(2,3))
should return
S2 S3 S4
and so on. If the column would have been fixed (say, b), I could simply do:
col_metaData[col_metaData$b %in% inputList,]$sample
But since there can be many more columns(hence I can't use if-else), I was looking for a different method to do the same. Can someone please help me do this? Thanks...
I solved it. Just in case anyone comes looking for an answer, we can use this:
col_metaData[col_metaData[,b] %in% inputList,]$sample
Notice [,b] instead of $b.
Eiffel
Seems I'm getting an INTEGER_32_REF from a BIGINT from ODBC with Eiffel DB_SELECTION.cursor. Sometimes I seem to have the adecuate value, but sometimes not...
946383958 => good INTEGER_32_REF (id->4 of following table)
9475984837 => 886050245 WRONG INTEGER_32_REF (id->4 of following table)
646383958 => good INTEGER_32_REF (id->3 of following table)
6475984837 => -2113949755 WRONG INTEGER_32_REF (id->4 of following table)
...
It seems to behave like not treating the BIGINT and giving the INTEGER_32_REF value directly interpreted from the DB value, typical case passing over the 2^32
Postgresql
Having a table with following data as electricity_amount BIGINT NOT NULL DEFAULT 0
id | a_date | amount | electricity_amount | consumption_sector_id | electricity_rate_id
----+------------+-----------+--------------------+-----------------------+---------------------
1 | 2019-01-01 | 746383958 | 7475984837 | 1 | 1
2 | 2019-02-01 | 846383958 | 8475984837 | 1 | 1
3 | 2019-03-01 | 646383958 | 6475984837 | 1 | 1
4 | 2019-04-01 | 946383958 | 9475984837 | 1 | 1
By default, not all the data types are mapped to Eiffel side.
So you will need to inherit from the class GLOBAL_SETTINGS and call the feature
set_use_extended_types (True) --| Use extended types: INTEGER_64, STRING_32 etc.
I have the following SQLite table
CREATE TABLE visits(urid INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT,dX INTEGER,dY INTEGER,dZ INTEGER);
Typical content would be
# select * from visits;
urid | hash | dx | dY | dZ
------+-----------+-------+--------+------
1 | 'abcd' | 10 | 10 | 10
2 | 'abcd' | 11 | 11 | 11
3 | 'bcde' | 7 | 7 | 7
4 | 'abcd' | 13 | 13 | 13
5 | 'defg' | 20 | 21 | 17
What I need to do here is identify the urid for the table row which satisfies the constraint
hash = 'abcd' AND (nearby >= (abs(dX - tX) + abs(dY - tY) + abs(dZ - tZ))
with the smallest deviation - in the sense of smallest sum of absolute distances
In the present instance with
nearby = 7
tX = tY = tZ = 12
there are three rows that meet the above constraint but with different deviations
urid | hash | dx | dY | dZ | deviation
------+-----------+-------+--------+--------+---------------
1 | 'abcd' | 10 | 10 | 10 | 6
2 | 'abcd' | 11 | 11 | 11 | 3
4 | 'abcd' | 12 | 12 | 12 | 3
in which case I would like to have reported urid = 2 or urid = 3 - I don't actually care which one gets reported.
Left to my own devices I would fetch the full set of matching rows and then dril down to the one that matches my secondary constraint - smallest deviation - in my own Java code. However, I suspect that is not necessary and it can be done in SQL alone. My knowledge of SQL is sadly too limited here. I hope that someone here can put me on the right path.
I now have managed to do the following
CREATE TEMP TABLE h1(v1 INTEGER,v2 INTEGER);
SELECT urid,(SELECT (abs(dX - 12) + abs(dY - 12) + abs(dZ - 12))) devi FROM visits WHERE hash = 'abcd';
which gives
--SELECT * FROM h1
urid | devi |
-------+-----------+
1 | 6 |
2 | 3 |
4 | 3 |
following which I issue
select urid from h1 order by v2 asc limit 1;
which yields urid = 2, the result I am after. Whilst this works, I would like to know if there is a better/simpler way of doing this.
You're so close! You have all of the components you need, you just have to put them together into a single query.
Consider:
SELECT urid
, (abs(dx - :tx) + abs(dy - :tx) + abs(dz - :tx)) AS devi
FROM visits
WHERE hash=:hashval AND devi < :nearby
ORDER BY devi
LIMIT 1
Line by line, first you list the rows and computed values you want (:tx is a placeholder; in your code you want to prepare a statement and then bind values to the placeholders before executing the statement) from the visit table.
Then in the WHERE clause you restrict what rows get returned to those matching the particular hash (That column should have an index for best results... CREATE INDEX visits_idx_hash ON visits(hash) for example), and that have a devi that is less than the value of the :nearby placeholder. (I think devi < :nearby is clearer than :nearby >= devi).
Then you say that you want those results sorted in increasing order according to devi, and LIMIT the returned results to a single row because you don't care about any others (If there are no rows that meet the WHERE constraints, nothing is returned).
When I order my database SQLITE by Classement I have this :
Classement | Nom
1 | clem
10 | caro
11 | flo
12 | raph
2 | prisc
3 | karim
4 | prout
I would like to get :
Classement | Nom
1 | clem
2 | prisc
3 | karim
4 | prout
10 | caro
11 | flo
12 | raph
Here is my code :
SELECT t.Classement
FROM tableau t
WHERE 1 = (SELECT 1 + COUNT (*) FROM tableau t2 WHERE t2.Classement < t.Classement OR ( t2.Classement == t.Classement AND t2.Nom < t.Nom ))
Can anyone help me ?
Thank you!
I guess column Classement is not an integer but character. So try this:
SELECT * FROM tableau ORDER BY cast(Classement as integer);
You get alphabetic order if the values are strings.
To change the table so that all Classement values are numbers, ensure that the column type is not a text type, and use this:
UPDATE tableau SET Classement = CAST(Classement AS NUMBER);
Suppose i have a datframe with values
Mtemp:
-----+
code |
-----+
Ram |
John |
Tracy|
Aman |
i want to compare it with dataframe
M2:
------+
code |
------+
Vivek |
Girish|
Rum |
Rama |
Johny |
Stacy |
Jon |
i want to get result so that for each value in Mtemp i will get maximum 2 possible match in M2 with Levensthein distance 2.
i have used
tp<-as.data.frame(amatch(Mtemp$code,M2$code,method = "lv",maxDist = 2))
tp$orig<-Mtemp$code
colnames(tp)<-c('Res','orig')
and i am getting result as follow
Res |orig
-----+-----
3 |Ram
5 |John
6 |Tracy
4 |Aman
please let me know a way to get 2 values(if possible) for every Mtemp string with Lev distance =2