If I have a column in a SQLite table like "Color" with values like "Red," "Blue," and "Green" and use ORDER BY Color, it'll automatically order the colors alphabetically.
Is there any way to specify the order of the colors? For instance, if I specified:
ORDER BY Color ('Green', 'Red', 'Blue')
to get the results ordered so the rows with Green are first, followed by the ones with Red are second, and so on?
For example, if I have this table:
Car Color
----------- ----
Mustang Red
Thunderbird Blue
DeLorean Blue
380SL Green
If I do a normal "SELECT Car FROM AntiqueCars ORDER BY Color," it would return:
Thunderbird
DeLorean
380SL
Mustang
(Although Thunderbird and DeLorean might end up swapped.)
I'd like to be able to use "SELECT Car FROM AntiqueCars ORDER BY Color(Red, Green, Blue)," and get:
Mustang
380SL
Thunderbird
DeLorean
Is there any way to do this kind of sorting with values that are normally not recognized as alphabetically or numerically (or temporally) sortable?
You can use CASE in your ORDER BY clause to specify custom ordering:
SELECT Car
FROM AntiqueCars
ORDER BY CASE WHEN Color = 'red' THEN 1 WHEN Color = 'green' THEN 2 ELSE 3 END
Related
I have two vectors of character values with the same size.
The first one is always the same four HEX color codes, while the second one is always the same four countries.
The only difference is that the countries may change order.
So for example I have ("Blue", "Red", "Yellow", "Green"), always in the same order.
While the second one can vary, and it could be for example ("Spain","Italy","Germany","Japan") or it could be ("Italy","Japan", "Germany",Spain")
However, I want the colors to be always associated with the same country.
What I though about was reordering the hex code colors based on the order of the countries.
So Spain would be #1 and then blue would be #1.
If Spain changes places to, let's say #3 then blue would become #3.
I tried with
colors[match(colors, countries)]
But it's not working.
I have sorted table according to number column as follows:
color - string
number - integer
I need to select those raws, where the number is by 10 or more bigger than the previous one of the same color.
So for example:
color number
black 1
blue 2
black 6
black 20
black 21
blue 22
blue 23
should return:
black 20
blue 22
Please, could you suggest me how to write such a query in SQLite? Or at least give me some hints on how to think about the problem?
Use LAG() window function to check the previous number of the same color for each row:
SELECT color, number
FROM (
SELECT *, LAG(number) OVER (PARTITION BY color ORDER BY number) prev_number
FROM tablename
)
WHERE number - prev_number > 10;
See the demo.
Seaborn has an option to create nice color palettes. I wish to use these palettes to generate colors that work well together in a map where countries are shaded according to some property. The following code produces 8 shades of purple from light to dark. Note also the ability to specify the number of colors is required so I cannot just use a fixed palette of defined colors.
import seaborn as sns
num_shades = 8
sns.palplot(sns.cubehelix_palette(num_shades))
If I run the same but in a list like so:
color_list = sns.cubehelix_palette(num_shades)
you get:
[[0.9312692223325372, 0.8201921796082118, 0.7971480974663592], ...
These are clearly not RGB values which is what I need.
1) What format are these colors in?
2) How can I convert to RGB or 6 digit codes?
I have tried searching for quite some time and found no answers. I have looked here and at other seaborn documentation:
https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.set_color_codes.html
I can convert to 6 digit codes from RGB using:
Converting a RGB color tuple to a six digit code, in Python
but am stuck as to how to do it direct or via getting the RGB values. Any help would be appreciated.
If by "6 digit code" you mean a hex code, you can also do:
pal = sns.color_palette(...)
pal.as_hex()
The values you are getting are percentages of 255, the max RGB value.
Just multiply each triplet of values by 255 (and round off, if you like) to get the RGB values.
for color in color_list:
for value in color:
value *= 255
Then store those in a new list to have your list of RGB values.
Just adding a point to #mwaskom answer as I am not allowed to comment yet.
pal = sns.color_palette(...)
pal.as_hex()
This in my case was giving colors as shown in the image1 (using python 3.9),
image1
If you want color hex codes, you can slice it like this:
pal = sns.color_palette(...)
pal.as_hex()[:]
For a particular color, say the first color, use this:
pal = sns.color_palette(...)
pal.as_hex()[0]
I have a datagrid where I use a color theme to set the row hilite color:
set the dgprop["hilite color"] of group "dg_xxx" to color1 -- #dc8400
Now I would like to have the alternate row color based on color1 but with a lighter note (i.E. 30% lightened up is #FFD14D).
Is there a way to do something like the following with RGB or HEX calculations to make it dynamically changing when color1 changes?
set the dgprop["alternate row color"] of group "dg_xxx" to (color1 - 30%)
The following function returns a shade, which is calculated by adding a share s of the remaining distance to white. Warning 1: basically you increase the brightness by using this function and you may end up with an ugly type of grey and eventually white. Warning 2: s and -s don't give exactly reverse results.
function rgbShade
if the paramCount is 4 then
put param(1) into r
put param(2) into g
put param(3) into b
put param(4) into s
else
put item 1 of param(1) into r
put item 2 of param(1) into g
put item 3 of param(1) into b
put param(2) into s
end if
put r+(255-r)*s into r
put g+(255-g)*s into g
put b+(255-b)*s into b
return r,g,b
end rgbShade
Usage:
get rgbShade("123,45,67",0.2) // increase of 20% of remaining distance
put rgbShade(123,45,67,0.2) into myShade // same as above
Example:
set the dgprop["hilite color"] of group "dg_xxx" to color1 -- #dc8400
set the dgprop["alternate row color"] of group "dg_xxx" to rgbShade(color1,.3)
I was wondering if anyone can help with grouping the data below as I'm trying to use the subset function to filter out volumes below a certain threshold but given that the data represents groups of objects, this creates the problem of removing certain items that should be kept.
In Column F ( and I) you can see Blue, Red, and Yellow Objects. Each represent three separate colored probes on one DNA strand. Odd numbered or non-numbered Blue ,Red, and Yellow are paired with a homologous strand represented by an even numbered Blue, Red, and Yellow. Ie data in rows 2,3,and 4 are one "group" and pair with the "group" shown in rows 5,6,and 7. This then repeats, so 8,9,10 are a new group and that group pairs with the one in 11,12,13.
What I would like to do is subset the groups so that only those below a certain Distance to Midpoint (column M) are kept. The Midpoint here is the midpoint of the line that connects the blue of one group with the blue of its partner, so the subset should only apply to the Blue distance to midpoint, and that is where I'm having a problem. For instance if I ask to keep blue distances to midpoint that are less than 3, then the objects in row 3 and 4 should be kept because they are part of the group with the blue distance below 3. Right now though when I filter with the subset function I lose Red Selection and Yellow Selection. I'm confident there is a straighforward solution to this in R, but I'd also be open to some type of filtering in excel if anyone has suggestions via that route instead.
EDIT
I managed to work something out in Excel last night after posting the question. Solution isn't pretty but it works well enough. I just added a new column next to "distance to midpoint" that gives all the objects in one group the same distance so that when I filter the data I won't lose any objects that I shouldn't. If it helps anyone in the future, the formula I used in excel was:
=SQRT ( ((INDEX($B$2:$B$945,1+QUOTIENT(ROWS(B$2:B2)-1,3)*3))- (INDEX($O$2:$O$945,1+QUOTIENT(ROWS(O$2:O2)-1,3)*3)) ) ^2 +( (INDEX($C$2:$C$945,1+QUOTIENT(ROWS(C$2:C2)-1,3)*3))-(INDEX($P$2:$P$945,1+QUOTIENT(ROWS(P$2:P2)-1,3)*3)) ) ^2 +( (INDEX($D$2:$D$945,1+QUOTIENT(ROWS(D$2:D2)-1,3)*3))-(INDEX($Q$2:$Q$945,1+QUOTIENT(ROWS(Q$2:Q2)-1,3)*3)) ) ^2)
Would be easier with a reproducible example, but here's a (hacky) plyr solution:
filterframe<-function(df,threshold){
df$grouper<-rep(seq(from=1,to=6),nrow(df)/6)
dataout<-df%>%group_by(grouper)%>%summarise(keep=.[[1]]$distance_to_midpoint<threshold)
dataout[dataout$keep,]
}
filterframe(mydata)
A base R solution provided below. The idea is that once your data are in R, you (edit) keep! rows iff they meet 2 criteria. First, the Surpass column has to contain the word "blue" in it, which is done with the grepl function. Second, the distance must below a certain threshold (set arbitrarily by thresh.
fakeData=data.frame(Surpass=c('blue', 'red', 'green', 'blue'),
distance=c(1,2,5,3), num=c(90,10,9,4))
#thresh is your distance threshold
thresh = 2
fakeDataNoBlue = fakeData[which(grepl('blue', fakeData$Surpass)
& fakeData$distance < thresh),]
There's probably also a quick dplyr solution using filter, but I haven't fully explored the functionality there. Also, I may be a bit confused on if you also want to keep the other colors. If so, that's the same as saying you want to remove the blue ones exceeding a certain distance threshold, which you would just do a -which command, and turn the < operator into a > operator.