How to use while loop in R to generate a matrix with specific number?(for->while) - while-loop

I have generated a matrix by using the following for loop.
And now I am trying to generate a same matrix using while loop but don't know how to do so.
Can anyone help with this? Thank you so much.
a<-matrix(0, ncol=9, nrow=9)
for(i in 1:9) {
for(j in 1:9) {
a[i,j]<-i*j
}
}
a
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81

i<-1
j<-1
a<-matrix(0, ncol=9, nrow=9)
while (i<=9) {
+ while (j<=9) {
+ a[i,j]<-I*j
+ j<-j+1
+ }
+ i<-i+1
+ j<-1
+ }

Related

How can i change my AMPL mod/data file for Capacity to not get syntax error

My problem from NEOS server comes out as:
amplin, line 16 (offset 239):
syntax error
context: param Capacity{i in >>> 1...m <<< };
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Data is:
param m := 4;
param n := 30;
param Facilitycost:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 :=
1 2 7 7 6
2 8 3 1 5
3 2 5 6 6
4 7 5 2 3
5 5 3 6 8
6 9 8 5 1
7 4 4 6 7
8 8 4 8 11
9 10 5 2 5
10 1 8 9 9
11 7 1 5 8
12 1 7 8 8
13 1 7 8 8
14 7 1 3 6
15 3 5 8 8
16 8 1 4 8
17 7 1 3 6
18 7 3 2 4
19 10 3 3 7
20 4 4 7 9
21 4 5 5 4
22 6 6 4 2
23 9 6 2 3
24 6 4 8 10
25 6 6 4 2
26 4 7 6 4
27 8 1 3 7
28 1 8 9 8
29 6 2 6 9
30 9 6 2 3;
param Capacity:= 1 5000 2 5000 3 5000 4 5000;
param Demand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 :=
1 220
2 374
3 351
4 432
5 161
6 300
7 300
8 219
9 339
10 312
11 653
12 440
13 207
14 492
15 91
16 190
17 351
18 323
19 23
20 157
21 281
22 233
23 409
24 215
25 7
26 680
27 215
28 395
29 165
30 333;
My model is:
param m; #number of facilites
param n; #number of customers needed to be served
param Facilitycost{j in 1..n, i in 1..m};
param Capacity{i in 1...m};
param Demand{j in 1..n};
#THE DECISION VARIABLES
var AllocatedFacility{j in 1..n, i in 1..m} binary;
#OBJECTIVE FUNCTION
minimize Total_AllocationCost: sum {j in 1..n, i in 1..m}:
Facilitycost[j,i] * AllocatedFacility[j,i];
#THE CONSTRAINTS
s.t. CapacityConstraints {i in 1..m}:
Demand[j] * AllocatedFacility[j,i] <= Capacity[i];
s.t. AllocatedFacilityContraints {j in 1..n}:
sum {i in 1..m} AllocatedFacility[j,i] = 1;
How can i change Capacity as such that the condition is met as wanted for the colum i?
Is the problem in the data of in the model?
The syntax error for this line is because you have 1...m where you should have 1..m.

SQL Server : create new column category price according to price column

I have a SQL Server table with a column price looking like this:
10
96
64
38
32
103
74
32
67
103
55
28
30
110
79
91
16
71
36
106
89
87
59
41
56
89
68
32
80
47
45
77
64
93
17
88
13
19
83
12
76
99
104
65
83
95
Now my aim is to create a new column giving a category from 1 to 10 to each of those values.
For instance the max value in my column is 110 the min is 10. Max-min = 100. Then if I want to have 10 categories I do 100/10= 10. Therefore here are the ranges:
10-20 1
21-30 2
31-40 3
41-50 4
51-60 5
61-70 6
71-80 7
81-90 8
91-100 9
101-110 10
Desired output:
my new column called cat should look like this:
price cat
-----------------
10 1
96 9
64 6
38 3
32 3
103 10
74 7
32 3
67 6
103 10
55 5
28 2
30 3
110 10
79 7
91 9
16 1
71 7
36 3
106 10
89 8
87 8
59 5
41 4
56 5
89 8
68 6
32 3
80 7
47 4
45 4
77 7
64 6
93 9
17 1
88 8
13 1
19 1
83 8
12 1
76 7
99 9
104 10
65 6
83 8
95 9
Is there a way to perform this with T-SQL? Sorry if this question is maybe too easy. I searched long time on the web. So either the problem is not as simple as I imagine. Either I entered the wrong keywords.
Yes, almost exactly as you describe the calculation:
select price,
1 + (price - min_price) * 10 / (max_price - min_price + 1) as decile
from (select price,
min(price) over () as min_price,
max(price) over () as max_price
from t
) t;
The 1 + is because you want the values from 1 to 10, rather than 0 to 9.
Yes - a case statement can do that.
select
price
,case
when price between 10 and 20 then 1
when price between 21 and 30 then 2
when price between 31 and 40 then 3
when price between 41 and 50 then 4
when price between 51 and 60 then 5
when price between 61 and 70 then 6
when price between 71 and 80 then 7
when price between 81 and 90 then 8
when price between 91 and 100 then 9
when price between 101 and 110 then 10
else null
end as cat
from [<enter your table name here>]

How to divide a result set into equal parts?

I have a table new_table
ID PROC_ID DEP_ID OLD_STAFF NEW_STAFF
1 15 43 58 ?
2 19 43 58 ?
3 29 43 58 ?
4 31 43 58 ?
5 35 43 58 ?
6 37 43 58 ?
7 38 43 58 ?
8 39 43 58 ?
9 58 43 58 ?
10 79 43 58 ?
How I can select all proc_ids and update new_staff, for example
ID PROC_ID DEP_ID OLD_STAFF NEW_STAFF
1 15 43 58 15
2 19 43 58 15
3 29 43 58 15
4 31 43 58 15
5 35 43 58 23
6 37 43 58 23
7 38 43 58 23
8 39 43 58 28
9 58 43 58 28
10 79 43 58 28
15 - 4(proc_id)
23 - 3(proc_id)
28 - 3(proc_id)
58 - is busi
where 15, 23, 28 and 58 staffs in one dep
"how to divide equal parts"
Oracle has a function, ntile() which splits a result set into equal buckets. For instance this query puts your posted data into four buckets:
SQL> select id
2 , proc_id
3 , ntile(4) over (order by id asc) as gen_staff
4 from new_table;
ID PROC_ID GEN_STAFF
---------- ---------- ----------
1 15 1
2 19 1
3 29 1
4 31 2
5 35 2
6 37 2
7 38 3
8 39 3
9 58 4
10 79 4
10 rows selected.
SQL>
This isn't quite the solution you want but you need to clarify your requirements before it's possible to provide a complete answer.
update new_table
set new_staff='15'
where ID in('1','2','3','4')
update new_table
set new_staff='28'
where ID in('8','9','10')
update new_table
set new_staff='23'
where ID in('5','6','7')
Not sure if this is what you mean.

How to use pd.cut in pandas

Can anyone help me figure out why this isn't working:
ages = ['15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84']
race['age_group'] = pd.cut(race.Age,range(13,84,5),right=False, labels=ages)
race[['Age','age_group']].head(15)
This is the result I get:
Age age_group
0 31 30-34
1 38 40-44
2 45 45-49
3 30 30-34
4 45 45-49
5 35 35-39
6 32 30-34
7 33 35-39
8 29 30-34
9 42 40-44
10 34 35-39
11 48 50-54
12 35 35-39
13 51 50-54
14 38 40-44
Your "range" is not correct, try:
ages = ['15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84']
race['age_group'] = pd.cut(race.Age,range(15,86,5),right=False, labels=ages)
race[['Age','age_group']].head(15)

trying to get user assign to under another user

I am trying to write a sql query in which I want all the list of uid assigned under another uid from the below given table
uid rid assTo
1 1 NULL
2 2 1
3 1 2
6 11 3
7 11 1
17 11 1
18 11 1
19 11 1
21 11 1
22 2 1
23 11 22
24 2 22
25 10 24
26 10 24
27 11 26
28 11 26
29 10 24
30 11 3
31 11 29
32 11 29
33 11 29
34 11 29
35 11 29
36 11 29
37 11 29
38 11 29
39 11 29
40 11 29
41 11 29
47 11 2
48 11 2
50 11 26
51 11 2
52 11 26
53 11 29
55 11 1
56 11 1
57 11 652
68 11 652
70 11 652
71 11 652
72 11 2
74 1 1
75 11 2
76 11 652
80 11 652
86 11 652
87 11 1
88 11 26
89 11 29
I want all the list of uid assigned to another uid i.e. designated in assto column in above table
How can I implement it?
My query
select u1.assignto 'assignto',u2.userid 'userid'
from users u1 join users u2
on u1.assignto=u2.userid
My desired output is that when i search uid = 1
i should the list of the uid available in the above table.
IN solution:
select * from tablename
where uid in (select assTo from tablename)
Self JOIN solution:
select distinct t1.*
from tablename t1
JOIN tablename t2 ON t1.uid= t2.assTo
EXISTS solution (similar to IN):
select *
from tablename t1
where exists (select 1 from tablename t2
where t2.assTo = t1.uid)