I'm trying to figure out a way to calculate the positions in the grid like I have below. I know the row, column, totalColumns, totalRows. For example, given column = 2, row = 0, totalColumns = 4, totalRows = 3, the position is B (11)
Cols
+ + + + +
| 0 | 1 | 2 | 3 |
+--+---|---|---|---|---
0 | 9 | A | B | C |
+--+---|---|---|---|--- Rows
1 | 5 | 6 | 7 | 8 |
+--+---|---|---|---|---
2 | 1 | 2 | 3 | 4 |
+--+---|---|---|---|---
ah,, well, i guess you have better thinks to do than school ;))
hex(tr*tc-r*tc-tc+c+1)
Related
In R, I've created a 3-dimensional table from a dataset. The three variables are all factors and are labelled H, O, and S. This is the code I used to simply create the table:
attach(df)
test <- table(H, O, S)
Outputting the flattened table produces this table below. The two values of S were split up, so these are labelled S1 and S2:
ftable(test)
+-----------+-----------+-----+-----+
| H | O | S1 | S2 |
+-----------+-----------+-----+-----+
| Isolation | Dead | 2 | 15 |
| | Sick | 64 | 20 |
| | Recovered | 153 | 379 |
| ICU | Dead | 0 | 15 |
| | Sick | 0 | 2 |
| | Recovered | 1 | 9 |
| Other | Dead | 7 | 133 |
| | Sick | 4 | 20 |
| | Recovered | 17 | 261 |
+-----------+-----------+-----+-----+
The goal is to use this table object, subset it, and produce a second table. Essentially, I want only "Isolation" and "ICU" from H, "Sick" and "Recovered" from O, and only S1, so it basically becomes the 2-dimensional table below:
+-----------+------+-----------+
| | Sick | Recovered |
+-----------+------+-----------+
| Isolation | 64 | 153 |
| ICU | 0 | 1 |
+-----------+------+-----------+
S = S1
I know I could first subset the dataframe and then create the new table, but the goal is to subset the table object itself. I'm not sure how to retrieve certain values from each dimension and produce the reduced table.
Edit: ANSWER
I now found a much simpler method. All I needed to do was reference the specific columns in their respective directions. So a much simpler solution is below:
> test[1:2,2:3,1]
O
H Sick Healed
Isolation 64 153
ICU 0 1
Subset the data before running table, example:
ftable(table(mtcars[, c("cyl", "gear", "vs")]))
# vs 0 1
# cyl gear
# 4 3 0 1
# 4 0 8
# 5 1 1
# 6 3 0 2
# 4 2 2
# 5 1 0
# 8 3 12 0
# 4 0 0
# 5 2 0
# subset then run table
ftable(table(mtcars[ mtcars$gear == 4, c("cyl", "gear", "vs")]))
# vs 0 1
# cyl gear
# 4 4 0 8
# 6 4 2 2
I have a simple database table with three columns: id, x, y. x and y are just the coordinates of points in a line. I want to using the SQLite Window function to partition the table using a sliding window of three rows, and then get the y value that is the furthest from the y value of the first coordinate (row) in the window.
An example:
| id | x | y |
|----|---|---|
| 1 | 1 | .5|
| 2 | 2 | .9|
| 3 | 3 | .7|
| 4 | 4 |1.1|
| 5 | 5 | 1 |
So the first partition would consist of:
| id | x | y |
|----|---|---|
| 1 | 1 | .5|
| 2 | 2 | .9|
| 3 | 3 | .7|
And the desired result would be:
| id | x | y | d |
|----|---|---|---|
| 1 | 1 | .5| .4|
| 2 | 2 | .9|
| 3 | 3 | .7|
Since the the window with id = 1 as the CURRENT ROW would have a maximum variation of .4; the maximum distance between the y value of the first row in the partition, .5, and .9, is .4.
The final expected result:
| id | x | y | d |
|----|---|---|---|
| 1 | 1 | .5| .4|
| 2 | 2 | .9| .2|
| 3 | 3 | .7| .4|
| 4 | 4 |1.1| .1|
| 5 | 5 | 1 | |
I've tried using a window function like: WINDOW win1 AS (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING which gives me the correct window.
With the window defined, I tried doing something like:
SELECT
max(abs(y - first_value(y) OVER win1)) AS d
FROM t
WINDOW win1 AS (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING)
But I get an error for misuse of first_value.
I think the problem I have is this is not the proper approach to calculate over each row of a partition, but I could not find another solution or approach that matches what I am trying to do here.
For each row of your table you define a window starting from the current row up to the next 2 rows.
In your code y is the value in the current row and first_value() is the 1st value of y of the current window which is also the value of y of the current row.
So even if your code was syntactically correct the difference you calculate would always return 0.
It's easier to solve your problem with LEAD() window function:
WITH cte AS (
SELECT *,
LEAD(y, 1) OVER () AS y1,
LEAD(y, 2) OVER () AS y2
FROM tablename
)
SELECT
id, x, y,
MAX(ABS(y - y1), COALESCE(ABS(y - y2), 0)) d
FROM cte
See the demo.
Results:
id x y d
1 1 0.5 0.4
2 2 0.9 0.2
3 3 0.7 0.4
4 4 1.1 0.1
5 5 1.0
If got data looking like this:
A | B | C
--------------
f | 1 | 1420h
f | 1 | 1540h
f | 3 | 600h
g | 2 | 900h
g | 2 | 930h
h | 1 | 700h
h | 3 | 400h
Now I want to create a new column which counts other rows in the data frame that meet certain conditions.
In this case I would like to know in each row how often the same combination of A and B occured in a range of 100 around C.
So the result with this data would be:
A | B | C | D
------------------
f | 1 | 1420 | 0
f | 1 | 1540 | 0
f | 3 | 1321 | 0
g | 2 | 900 | 1
g | 2 | 930 | 1
h | 1 | 700 | 0
h | 3 | 400 | 0
I actually came to a solution using for(for()). But the time R needs to compute the resuts is tooooo long.
for(i in 1:nrow(df)) {
df[i,D] <- sum( for(p in 1:nrow(df)) {
df[p,A] == df[i,A] &
df[p,B] == df[i,B] &
df[i,C] +100 > df[p,C] &
df[p,C] > df[i,C]-100 } ) }
Is there a better way?
Thanks a lot!
I am new to Julia and try to get a simple x-y plot through Gadfly Pkg.
I am trying to plot x-axis in log scale and set min and max value in the same time.
plot(layer(rdsPmos, x="A", y="B", Geom.line), Scale.x_log10(minvalue= 10),
Theme(default_point_size = 1.5px))
This won't get any error message. The outcome plot has a log scale x-axis but the minvalue seems not work.
I also try to write lise this:
plot(layer(rdsPmos, x="A", y="B", Geom.line), Scale.x_log10, Scale.x_continuous(minvalue= 10), Theme(default_point_size = 1.5px))
And the result is the minvalue work but the logscale fail.
My tests shows that minvalue and maxvalue options works in the way that none of the data missed from view-port, (true for x_continuous or x_log10), so if one wants a narrower view-port, one way is to apply filter on data:
julia> df = DataFrame(A = 1:10, B = 2:2:20)
10x2 DataFrames.DataFrame
| Row | A | B |
|-----|----|----|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 8 |
| 5 | 5 | 10 |
| 6 | 6 | 12 |
| 7 | 7 | 14 |
| 8 | 8 | 16 |
| 9 | 9 | 18 |
| 10 | 10 | 20 |
minvalue is not working, and it's nothing with Scale type:
julia> plot(layer(df, x="A", y="B" ,Geom.line), Scale.x_log10(minvalue=5), Theme(default_point_size = 1.5px))
julia> plot(layer(df, x="A", y="B" ,Geom.line), Scale.x_continuous(
minvalue=5), Theme(default_point_size = 1.5px))
minvalue is working on filtered data
julia> plot(layer(df[df[:A].>5,:], x="A", y="B" ,Geom.line), Scale.x_log10(minvalue=5), Theme(default_point_size = 1.5px))
I have one table 'positions' with columns:
id | session_id | keyword_id | position
and some rows in it:
10 rows with session_id = 1
and 10 with session_id = 2.
As a result of the query I need a table like this:
id | keyword_id | position1 | position2
where 'position1' is a column with values that had session_id = 1 and 'position2' is a column with values that had session_id = 2.
The result set should contain 10 records.
Sorry for my bad English.
Data examle:
id | session_id | keyword_id | position
1 | 1 | 1 | 2
2 | 1 | 2 | 3
3 | 1 | 3 | 0
4 | 1 | 4 | 18
5 | 2 | 5 | 9
6 | 2 | 1 | 0
7 | 2 | 2 | 14
8 | 2 | 3 | 2
9 | 2 | 4 | 8
10 | 2 | 5 | 19
Assuming that you wish to combine positions with the same id, from the two sessions, then the following query should to the trick:
SELECT T1.keyword_id
, T1.position as Position1
, T2.position as Position2
FROM positions T1
INNER JOIN positions T2
ON T1.keyword_id = T2.keyword_id -- this will match positions by [keyword_id]
AND T1.session_id = 1
AND T2.session_id = 2