Group columns and separate in R - columnsorting

I have a dataset, where I want to group 2 columns together (Proc Right and Days to Proc Right) and separate it from the next group of 2 columns (Proc Left and Days to Proc Left). During separation, I want to separate based on chronology of days to procedure, and assign 0 and NA to the other 2 columns which chronologically are later. I then want to create a new column pulling only the days to procedure.
To summarise:
Have this:
ID
Proc ID
Proc Right
Days to Proc Right
Proc Left
Days to Proc Left
1
108
4
41
4
168
1
105
4
169
4
42
1
101
3
270
0
NA
Want this:
ID
Proc ID
Proc Right
Days to Proc Right
Proc Left
Days to Proc Left
Days to Proc
1
108
4
41
0
NA
41
1
108
0
NA
4
168
168
1
105
0
NA
4
42
42
1
105
4
169
0
NA
169
1
101
3
270
0
NA
270
Would appreciate any help. Thanks
I have tried unite and cSplit, which separates the column groups, but doesn't help me assign 0 and NA to the other columns.

Related

Amazon QuickSight - Running Difference

I have the following table and want to add a column with running difference.
time_gap
amounts
0
150
0.5
19
1.5
2
6
1
7
4
my desired out is
time_gap
amounts
diff
0
150
150
0.5
19
131
1.5
2
129
6
10
119
7
4
115
What I've tried:
I duplicate the amounts column and used table calculation, difference, but got the difference between two consecutive rows instead:
time_gap
amounts
diff
0
150
0.5
19
-131
1.5
2
-17
6
10
8
7
4
-6
I tried some calculated fields formulas but that didn't work either.
thank you!

Make a matrix B of the first, fourth and fifth row and the first and fifth column from matrix A in OCTAVE

I have matrix A
A =
5 10 15 20 25
10 9 8 7 6
-5 -15 -25 -35 -45
1 2 3 4 5
28 91 154 217 280
And i need to make a matrix B of the first, fourth and fifth row and the first and fifth column from matrix A.
How can i do it?
>> B = A([1,4,5],[1,5])
B =
5 25
1 5
28 280
You should look up how to use index expressions in the Matlab and Octave language to extract and work with submatrices.
See the Octave help on Index expressions: https://octave.org/doc/latest/Index-Expressions.html

How to find the index of the first row of a matrix that satisfies two conditions in APL Language?

One more question to learn how to use APL Language.
Suppose you have an array, as an example:
c1
c2
c3
c4
c5
c6
3
123
0
4
5
6
3
134
0
2
3
4
3
231
180
1
2
5
4
121
0
3
2
4
4
124
120
4
6
3
4
222
222
5
3
5
So, how to find out which row has a value of 4 in the 1st column and a value grather than 0 in the 3rd column?
The expected answer is 5th line, in the just 5
When you want to make such "queries", think Boolean masks.
table ← 6 6⍴3 123 0 4 5 6 3 134 0 2 3 4 3 231 180 1 2 5 4 121 0 3 2 4 4 124 120 4 6 3 4 222 222 5
Let's extract the first column:
table[;1]
3 3 3 4 4 4
And indicate which elements have a value of 4:
table[;1] = 4
0 0 0 1 1 1
Similarly, we can indicate which elements of column 3 have value greater than 0:
table[;3] > 0
0 0 1 0 1 1
Their intersection (logical AND) indicates all rows that fulfil your criteria:
(table[;1] = 4) ∧ (table[;3] > 0)
0 0 0 0 1 1
The index of the first 1 is the row number for the first row that fulfils your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⍳ 1
5
Try it online!
Alternatively, we can use the final mask to filter the table and obtain all rows that fulfil your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⌿ table
4 124 120 4 6 3
4 222 222 5 3 5
Try it online!
Or we can generate all the row numbers:
⍳ 1 ↑ ⍴ table
1 2 3 4 5 6
Then use our Boolean mask to filter that, finding the row numbers of all the rows that fulfil your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⌿ ⍳ 1 ↑ ⍴ table
5 6
Try it online!

The groupby function didn't work on two dimensional array in laravel 5.5

id p_id approve m_approve
1 75 1 0
2 74 1 1
3 73 1 1
4 72 1 1
5 75 1 1
6 73 0 1
7 71 1 0
8 70 1 1
9 69 0 1
10 75 0 0
11 75 0 0
12 75 0 0
13 75 1 0
14 75 1 0
15 75 0 1
$result = DB::table('a16s_likes')
->select ('id','p_id','approve','m_approve')
->get() ///become collection
->groupBy('p_id')
->toarray(); //->all()
echo '<pre>' ;
print_r($result);
I got the right one dimentional array
But when I use
->groupBy('p_id','approve')
->all();
I got the same one dimensional array result?
not got the two dimentional array?
How can I get p_id(75)-approve(0) and p_id(75)-approve(1) two group and just take last 2 rows?
I fix the code
->groupBy(['p_id','m_approve'])
got
According to the DOCS
Multiple grouping criteria may be passed as an array. Each array element will be applied to the corresponding level within a multi-dimensional array
Which means that your code must be ->groupBy(['p_id','approve'])

R - how to pick a random sample with specific percentages

This is snapshot of my dataset
A B
1 34
1 33
1 66
0 54
0 77
0 98
0 39
0 12
I am trying to create a random sample where there are 2 1s and 3 0s from column A in the sample along with their respective B values. Is there a way to do that? Basically trying to see how to get a sample with specific percentages of a particular column? Thanks.

Resources