I have a table that has 3 columns
STA (which is equivalent to X)
BL (which is equivalent to y)
Ultimate_Load (which has positive and negative values)
I want to aggregate column 3 by the maximum of absolute values, but display the actual (negative or positive) value.
your help is appreciated
I think this would work. You get de max absolute value of ultimate_load and then change it's sign depending on the sign of MAX(ultimate_load)+MIN(ultimate_load).
SELECT STA,BL, MAX(ABS(ULTIMATE_LOAD)) * case sign(MAX(ULTIMATE_LOAD)+min(ULTIMATE_LOAD)) when 0 then 1 when -1 then -1 else 1 end as maxvalue
FROM TAB
GROUP BY STA, BL
Related
I want to split a vector into several parts separated by the numeric value 0. For each part, cumulatively calculate the sum of the elements encountered so far. Negative numbers do not participate in the calculation.
For example, with the input [0,1,1,1,0,1,-1,1,1], I expect the result to be [0,1,2,3,0,1,1,2,3].
How to implement this in DolphinDB?
Use the DolphinDB built-in function cumPositiveStreak(X). Treat the negative elements in X as NULL values.
Script:
a = 1 2 -1 0 1 2 3
cumPositiveStreak(iif(a<0,NULL,a))
Execution result:
1 3 3 0 1 3 6
I'm trying to compute a new variable using 3 other variables. If all 3 conditions are positive, the new variable gives 1. My problem: if just 1 or 2 of these conditions are present, I get a value 0 when it needs to be a system missing.
In order to get SPSS to calculate a value only when you have values in all three variables you can use this:
if nmiss(E’sept_preTx, Eope’gem_preTx, TRpieksnelheid_t1)=0
DD_new=(E’sept_preTx < 10) & (Eope’gem_preTx >= 15) & (TRpieksnelheid_t1 > 2.8).
the nmiss counts the missing values, and the original calculation is carried out only if tere are none.
I am trying to iterate over rows in a Pandas Dataframe using the itertuples()-method, which works quite fine for my case. Now i want to check if a specific value ('x') is in a specific tuple. I used the count() method for that, as i need to use the number of occurences of x later.
The weird part is, for some Tuples that works just fine (i.e. in my case (namedtuple[7].count('x')) + (namedtuple[8].count('x')) ), but for some (i.e. namedtuple[9].count('x')) i get an AttributeError: 'int' object has no attribute 'count'
Would appreciate your help very much!
Apparently, some columns of your DataFrame are of object type (actually a string)
and some of them are of int type (more generally - numbers).
To count occurrences of x in each row, you should:
Apply a function to each row which:
checks whether the type of the current element is str,
if it is, return count('x'),
if not, return 0 (don't attempt to look for x in a number).
So far this function returns a Series, with a number of x in each column
(separately), so to compute the total for the whole row, this Series should
be summed.
Example of working code:
Test DataFrame:
C1 C2 C3
0 axxv bxy 10
1 vx cy 20
2 vv vx 30
Code:
for ind, row in df.iterrows():
print(ind, row.apply(lambda it:
it.count('x') if type(it).__name__ == 'str' else 0).sum())
(in my opinion, iterrows is more convenient here).
The result is:
0 3
1 1
2 1
So as you can see, it is possible to count occurrences of x,
even when some columns are not strings.
I have an output of ranges from 150-0. I want to map those to 0 to 1. Or perhaps 0 to (some value less than 1 such as 0.5) where 150 is 0 and 0 is 1 ( or some values less than..).
Is this considered interpolation? What is the formula to derive these values? But preferably, is there a built-in StdLib function I can call?
Divide your number by the (Max - min). This would make 150 be 1 and 0 will be 0, with everything else a number in between. Now, to make it the opposite just do 1 - result.
If you need to map 0-1 to any custom range, you need to multiply range with MAX-MIN and then add MIN to it to get the exact number in range.
Formula will be MIN + (MAX-MIN)*value
where value is range in between 0-1;
MIN is number mapped to 0;
MAX is number mapped to 1;
Ok so I am trying to reference one variable with another in SQL.
X= a,b,c,d (x is a string variable with a list of things in it)
Y= b ( Y is a string variable that may or may not have a vaue that appears in X)
I tried this:
Case when Y in (X) then 1 else 0 end as aa
But it doesnt work since it looks for exact matches between X and Y
also tried this:
where contains(X,#Y)
but i cant create Y globally since it is a variable that changes in each row of the table.( x also changes)
A solution in SAS would also be useful.
Thanks
Maybe like will help
select
*
from
t
where
X like ('%'+Y+'%')
or
select
case when (X like ('%'+Y+'%')) then 1 else 0 end
from
t
SQLFiddle example
In SAS I would use the INDEX function, either in a data step or proc sql. This returns the position within the string in which it finds the character(s), or zero if there is no match. Therefore a test if the value returned is greater than zero will result in a binary 1:0 output. You need to use the compress function with the variable containing the search characters as SAS pads the value with blanks.
Data step solution :
aa=index(x,compress(y))>0;
Proc Sql solution :
index(x,compress(y))>0 as aa