This question already has answers here:
replace multiple values in a column for a single one
(3 answers)
Closed 2 years ago.
I am trying to create a new variable that contains a substitution of where is "NA", "A", "Q", "T" in the variable Var1 by 0 until line 143 and leave the others analyzed as they are normally, of the lines 144 through 164, and after line 164 assign 0 in the other letters "A", "Q", "T", using a mutate function from the dplyr package. However, I am not getting what I wanted.
Dados1 <- data.frame(Var1 = c(rep("NA", 60),
c(rep("H", 20), c(rep("Q", 30),c(rep("A", 33),
c("V","R","S","T","U","V","S",
"R","S","T","U","V","S",
"T","U","S","R","V","V","B"),
rep("NA", 30))))))
Dados1
library(dplyr)
Dados1 <- Dados1[c(1:143,),c(164:193,)] %>%
dplyr::mutate(NewCode = ifelse(x == c("NA","H","Q","A"), "0"))
Dados1
> Dados1 <- Dados1[c(1:143,),c(164:193,)] %>%
+ dplyr::mutate(NewCode = ifelse(x == c("NA","H","Q","A"), "0"))
Error in c(164:193, ) : argumento 2 está vazio
Maybe try this:
library(dplyr)
#Code
Dados1 <- Dados1 %>%
dplyr::mutate(NewCode = ifelse(Var1 %in% c("NA","H","Q","A"), "0",Var1))
Output:
Dados1
Var1 NewCode
1 NA 0
2 NA 0
3 NA 0
4 NA 0
5 NA 0
6 NA 0
7 NA 0
8 NA 0
9 NA 0
10 NA 0
11 NA 0
12 NA 0
13 NA 0
14 NA 0
15 NA 0
16 NA 0
17 NA 0
18 NA 0
19 NA 0
20 NA 0
21 NA 0
22 NA 0
23 NA 0
24 NA 0
25 NA 0
26 NA 0
27 NA 0
28 NA 0
29 NA 0
30 NA 0
31 NA 0
32 NA 0
33 NA 0
34 NA 0
35 NA 0
36 NA 0
37 NA 0
38 NA 0
39 NA 0
40 NA 0
41 NA 0
42 NA 0
43 NA 0
44 NA 0
45 NA 0
46 NA 0
47 NA 0
48 NA 0
49 NA 0
50 NA 0
51 NA 0
52 NA 0
53 NA 0
54 NA 0
55 NA 0
56 NA 0
57 NA 0
58 NA 0
59 NA 0
60 NA 0
61 H 0
62 H 0
63 H 0
64 H 0
65 H 0
66 H 0
67 H 0
68 H 0
69 H 0
70 H 0
71 H 0
72 H 0
73 H 0
74 H 0
75 H 0
76 H 0
77 H 0
78 H 0
79 H 0
80 H 0
81 Q 0
82 Q 0
83 Q 0
84 Q 0
85 Q 0
86 Q 0
87 Q 0
88 Q 0
89 Q 0
90 Q 0
91 Q 0
92 Q 0
93 Q 0
94 Q 0
95 Q 0
96 Q 0
97 Q 0
98 Q 0
99 Q 0
100 Q 0
101 Q 0
102 Q 0
103 Q 0
104 Q 0
105 Q 0
106 Q 0
107 Q 0
108 Q 0
109 Q 0
110 Q 0
111 A 0
112 A 0
113 A 0
114 A 0
115 A 0
116 A 0
117 A 0
118 A 0
119 A 0
120 A 0
121 A 0
122 A 0
123 A 0
124 A 0
125 A 0
126 A 0
127 A 0
128 A 0
129 A 0
130 A 0
131 A 0
132 A 0
133 A 0
134 A 0
135 A 0
136 A 0
137 A 0
138 A 0
139 A 0
140 A 0
141 A 0
142 A 0
143 A 0
144 V V
145 R R
146 S S
147 T T
148 U U
149 V V
150 S S
151 R R
152 S S
153 T T
154 U U
155 V V
156 S S
157 T T
158 U U
159 S S
160 R R
161 V V
162 V V
163 B B
164 NA 0
165 NA 0
166 NA 0
167 NA 0
168 NA 0
169 NA 0
170 NA 0
171 NA 0
172 NA 0
173 NA 0
174 NA 0
175 NA 0
176 NA 0
177 NA 0
178 NA 0
179 NA 0
180 NA 0
181 NA 0
182 NA 0
183 NA 0
184 NA 0
185 NA 0
186 NA 0
187 NA 0
188 NA 0
189 NA 0
190 NA 0
191 NA 0
192 NA 0
193 NA 0
Related
i have a problem figuring out how to check if a random bingo card is valid or not.I have a range of numbers of 1-90 and i want to create 6 cards of 15 numbers.I want to check if a card has 15 numbers with no duplicates,the numbers in the rows are in ascending order,and the column has to have 2 or 3 numbers.Meanwhile the columns of all the cards have to be in ascending order too but each column must be in a number range from 1-9,10-19...80-90.Here is an example of a valid bingo set of cards:
0 11 20 0 41 54 0 72 0
7 0 23 30 0 56 0 77 0
8 15 0 0 42 0 67 0 86
............................................
2 17 0 0 46 0 65 0 85
4 0 0 36 47 0 66 74 0
0 19 22 0 0 57 0 75 90
............................................
0 0 25 33 0 51 0 76 82
0 12 0 35 0 59 63 0 88
1 0 29 0 40 0 69 78 0
............................................
3 0 24 34 0 0 0 71 84
6 0 0 37 45 0 61 73 0
0 13 26 0 49 55 0 0 89
............................................
0 14 0 32 0 52 60 0 81
5 0 27 39 0 0 62 0 87
0 18 28 0 43 53 0 79 0
............................................
0 10 0 31 0 50 64 0 80
0 16 21 0 44 58 0 70 0
9 0 0 38 48 0 68 0 83
............................................
the zeroes represent the blank spaces.I dont know where to start since im a begginer.If you have any idea or tips that will be helpful please share it with me and ill be grateful to you.
Trying to use R cv.glmnet() for cross validation on loans data.
I have a data set on loan data (Kaggle) and have already split into train, test.
Separated the y response from the predictive variables in select(1) and select(-1).
Created matrix so as to avoid the "Error in storage.mode(y) <- "double" : 'list' object cannot be coerced to type 'double' " problem earlier.
Now seeking to run cv.glmnet() for cross validation, but this error stops me now.
"Error in y - predmat : non-numeric argument to binary operator"
Error in non-numeric argument, yet all my data is numeric, save for one factor for response y.
As a side question, what is the predmat in "y - predmat" refer to?
x_vars <- as.matrix(data.sample.train.split %>% select(-1))
y_resp <- as.matrix(data.sample.train.split %>% select(1))
cv_output <- cv.glmnet(x_vars, y_resp, type.measure = "deviance", nfolds = 5)
cv_output <- cv.glmnet(x_vars, y_resp,
type.measure = "deviance",
lambda = NULL,
nfolds = 5)
I am also considering to try this function:
ddd.lasso <- cv.glmnet(x_vars, y_resp, alpha = 1, family = "binomial")
ddd.model <- glmnet(x_vars, y_resp, alpha = 1, family = "binomial", lambda = ddd.lasso$lambda.min)
Data sample is as follows, just some of the columns:
c("loan_amnt", "funded_amnt",
"funded_amnt_inv", "grade", "emp_length", "annual_inc", "dti",
"mths_since_last_delinq", "mths_since_last_record", "open_acc",
"pub_rec", "revol_bal", "revol_util", "total_acc", "out_prncp",
"out_prncp_inv", "total_pymnt", "total_pymnt_inv", "total_rec_prncp",
"total_rec_int", "total_rec_late_fee", "recoveries", "collection_recovery_fee",
"last_pymnt_amnt", "collections_12_mths_ex_med", "acc_now_delinq"
)))
loan_amnt funded_amnt funded_amnt_inv grade emp_length annual_inc dti
3 10000 10000 10000.000 60 10 49200.00 20.00
10 10000 10000 10000.000 60 4 42000.00 18.60
14 20250 20250 19142.161 60 3 43370.00 26.53
17 15000 15000 15000.000 80 2 92000.00 29.44
18 4000 4000 4000.000 80 10 106000.00 5.63
31 4400 4400 4400.000 40 10 55000.00 20.01
35 10000 10000 10000.000 100 10 60000.00 12.74
37 25600 25600 25350.000 80 9 110000.00 15.71
41 10000 10000 10000.000 80 1 39000.00 18.58
64 9200 9200 9200.000 80 2 60000.00 19.96
72 7000 7000 7000.000 80 4 39120.00 21.01
74 3500 3500 3500.000 100 10 83000.00 2.31
77 9500 9500 9500.000 100 7 50000.00 8.18
89 10000 10000 10000.000 100 1 43000.00 25.26
98 7000 7000 7000.000 80 1 30000.00 15.80
112 21600 21600 20498.266 20 8 60000.00 16.74
117 7200 7200 7200.000 80 5 48000.00 17.43
118 12000 12000 11975.000 60 1 57000.00 10.86
125 10000 10000 10000.000 100 5 70000.00 16.78
126 8000 8000 8000.000 60 3 28000.00 12.60
128 6000 6000 6000.000 60 10 94800.00 24.53
138 35000 35000 35000.000 80 2 168000.00 3.17
144 14000 14000 14000.000 100 10 66000.00 11.15
149 3000 3000 3000.000 60 5 71000.00 21.84
152 12000 12000 11975.000 80 2 60000.00 15.50
153 6000 6000 6000.000 100 3 34000.00 14.51
155 7000 7000 7000.000 80 7 82000.00 12.00
166 24250 18100 18075.000 -1 7 120000.00 12.96
170 2500 2500 2500.000 80 7 29000.00 18.70
172 4225 4225 4225.000 80 5 55200.00 17.61
180 6000 6000 6000.000 60 5 50000.00 15.58
192 5000 5000 5000.000 80 5 38004.00 23.78
193 8000 8000 8000.000 80 3 31000.00 16.22
199 12000 12000 12000.000 80 4 40000.00 22.20
203 3200 3200 3200.000 80 9 61200.00 2.16
209 5000 5000 5000.000 80 2 70000.00 20.06
220 13250 13250 13250.000 40 10 52000.00 23.70
224 12000 12000 12000.000 100 10 68000.00 7.08
mths_since_last_delinq mths_since_last_record open_acc pub_rec revol_bal revol_util
3 35 59 10 0 5598 21.0
10 61 114 14 0 24043 70.2
14 18 107 8 0 17813 85.6
17 54 79 8 0 13707 93.9
18 18 97 12 0 6110 37.7
31 68 119 7 0 25237 99.0
35 37 93 11 0 14019 19.5
37 11 118 11 0 26088 62.0
41 58 17 5 0 12874 72.7
64 39 95 8 0 23299 78.7
72 26 33 8 1 9414 52.4
74 35 59 6 0 3092 13.4
77 46 118 8 0 13422 60.5
89 59 105 8 0 8215 37.2
98 68 101 7 0 15455 47.6
112 23 26 6 0 13354 78.1
117 24 19 7 0 16450 80.2
118 47 87 7 0 9273 81.5
125 32 92 9 0 10770 69.0
126 66 112 8 0 6187 54.3
128 10 101 13 0 71890 95.9
138 22 97 16 0 1099 1.4
144 26 102 7 0 12095 35.4
149 59 103 4 0 15072 88.7
152 46 94 7 0 12168 85.7
153 70 81 9 0 13683 64.8
155 79 83 6 0 25334 71.6
166 66 118 7 0 31992 99.0
170 63 99 5 0 2668 66.7
172 69 104 6 0 4055 73.7
180 49 94 8 0 7361 83.6
192 5 85 12 0 10023 57.3
193 28 77 13 0 2751 34.4
199 78 109 9 0 16273 55.5
203 79 113 5 1 2795 33.3
209 27 62 14 0 13543 54.2
220 70 86 8 0 15002 91.5
224 21 70 7 0 15433 55.6
total_acc out_prncp out_prncp_inv total_pymnt total_pymnt_inv total_rec_prncp
3 37 0 0 12226.302 12226.30 10000.00
10 28 0 0 12519.260 12519.26 10000.00
14 22 0 0 27663.043 25417.68 20250.00
17 31 0 0 15823.480 15823.48 15000.00
18 44 0 0 4484.790 4484.79 4000.00
31 11 0 0 5626.893 5626.89 4400.00
35 18 0 0 10282.670 10282.67 10000.00
37 27 0 0 29695.623 29405.63 25600.00
41 10 0 0 11474.760 11474.76 10000.00
64 19 0 0 10480.840 10480.84 9200.00
72 26 0 0 7932.300 7932.30 7000.00
74 28 0 0 3834.661 3834.66 3500.00
77 13 0 0 10493.710 10493.71 9500.00
89 16 0 0 11264.010 11264.01 10000.00
98 11 0 0 8452.257 8452.26 7000.00
112 21 0 0 27580.750 24853.63 21600.00
117 10 0 0 8677.156 8677.16 7200.00
118 11 0 0 14396.580 14366.62 12000.00
125 18 0 0 10902.910 10902.91 10000.00
126 11 0 0 8636.820 8636.82 8000.00
128 30 0 0 7215.050 7215.05 6000.00
138 22 0 0 38059.760 38059.76 35000.00
144 46 0 0 15450.084 15450.08 14000.00
149 14 0 0 3723.936 3723.94 3000.00
152 21 0 0 13919.414 13890.44 12000.00
153 16 0 0 6857.261 6857.26 6000.00
155 31 0 0 8290.730 8290.73 7000.00
166 20 0 0 22188.250 22157.63 18100.00
170 13 0 0 2894.740 2894.74 2500.00
172 12 0 0 5081.023 5081.02 4225.00
180 14 0 0 7325.299 7325.30 6000.00
192 17 0 0 6534.430 6534.43 5000.00
193 29 0 0 8306.470 8306.47 8000.00
199 23 0 0 14006.680 14006.68 12000.00
203 17 0 0 3709.193 3709.19 3200.00
209 26 0 0 5501.160 5501.16 5000.00
220 18 0 0 15650.390 15650.39 13250.00
224 34 0 0 12554.010 12554.01 12000.00
total_rec_int total_rec_late_fee recoveries collection_recovery_fee last_pymnt_amnt
3 2209.33 16.97000 0 0 357.48
10 2519.26 0.00000 0 0 370.46
14 7413.04 0.00000 0 0 6024.09
17 823.48 0.00000 0 0 2447.05
18 484.79 0.00000 0 0 2638.77
31 1226.89 0.00000 0 0 162.44
35 282.67 0.00000 0 0 8762.05
37 4095.62 0.00000 0 0 838.27
41 1474.76 0.00000 0 0 5803.94
64 1280.84 0.00000 0 0 365.48
72 932.30 0.00000 0 0 4235.03
74 334.66 0.00000 0 0 107.86
77 993.71 0.00000 0 0 5378.43
89 1264.01 0.00000 0 0 4.84
98 1452.26 0.00000 0 0 238.06
112 5980.75 0.00000 0 0 17416.49
117 1462.16 15.00000 0 0 19.26
118 2396.58 0.00000 0 0 5359.38
125 902.91 0.00000 0 0 4152.52
126 636.82 0.00000 0 0 6983.56
128 1215.05 0.00000 0 0 1960.88
138 3059.76 0.00000 0 0 272.59
144 1450.08 0.00000 0 0 2133.17
149 723.94 0.00000 0 0 107.29
152 1919.41 0.00000 0 0 395.05
153 857.26 0.00000 0 0 198.16
155 1290.73 0.00000 0 0 2454.29
166 4088.25 0.00000 0 0 16499.75
170 394.74 0.00000 0 0 1168.50
172 856.02 0.00000 0 0 146.48
180 1325.30 0.00000 0 0 215.51
192 1534.43 0.00000 0 0 1561.93
193 306.47 0.00000 0 0 7778.22
199 2006.68 0.00000 0 0 5971.51
203 509.19 0.00000 0 0 317.41
209 501.16 0.00000 0 0 3833.62
220 2400.39 0.00000 0 0 9026.78
224 554.01 0.00000 0 0 473.95
collections_12_mths_ex_med acc_now_delinq
3 0 0
10 0 0
14 0 0
17 0 0
18 0 0
31 0 0
35 0 0
37 0 0
41 0 0
64 0 0
72 0 0
74 0 0
77 0 0
89 0 0
98 0 0
112 0 0
117 0 0
118 0 0
125 0 0
126 0 0
128 0 0
138 0 0
144 0 0
149 0 0
152 0 0
153 0 0
155 0 0
166 0 0
170 0 0
172 0 0
180 0 0
192 0 0
193 0 0
199 0 0
203 0 0
209 0 0
220 0 0
224 0 0
Looks like a incorrect glmnet family, I accidently chose the default 'deviance' for cv.glmnet, when in fact my data was binomial. My next solution is to figure out "Convergence for 1th lambda value not reached after maxit=100000 iterations; solutions for larger lambdas returned"
Code that improved the solution:
cv.lasso <- cv.glmnet(x_vars, y_resp, alpha = 1, family = "binomial", nfolds = 5)
cv.model <- glmnet(x_vars, y_resp, alpha = 1, relax=TRUE, family="binomial", lambda=cv.lasso$lambda.min)
I know this question has been asked multiple times but I've run out of ideas to get the model working. The first 50 rows of the train data:
> train[1:25]
a b c d e f g h i j k l m
1: 0 148.00 27 16 0 A 0 117 92 0 13 271 2
2: 0 207.00 37 8 0 C 0 46 29 0 29 555 5
3: 0 1497.00 44 1 0 A 1 3754 2119 1 1961 5876 6
4: 0 463.00 44 1 0 A 0 287 202 0 105 1037 4
5: 0 19.00 82 1 0 A 0 301 186 0 344 2116 3
6: 0 204.00 41 1 0 A 0 92 76 0 290 1608 10
7: 0 79.00 69 16 0 B 0 48 29 0 1 27 3
8: 0 256.75 71 16 1 A 0 131 112 0 36 1183 0
9: 0 256.75 71 16 1 A 0 131 112 0 36 1183 2
10: 1 49.00 13 13 0 C 0 5 4 0 0 11 1
11: 0 19.00 76 1 0 A 0 897 440 0 575 2674 3
12: 0 49.00 100 100 0 C 0 6 6 0 0 0 1
13: 0 107.00 65 1 0 A 3 334 212 0 421 2773 6
14: 0 79.00 28 16 0 B 0 42 49 0 13 345 2
15: 0 1742.00 61 1 0 A 0 589 340 0 444 3853 8
16: 0 187.00 20 16 0 A 0 123 99 0 70 841 4
17: 0 68.00 73 1 0 A 0 757 507 0 359 773 3
18: 0 157.00 32 16 0 B 0 33 27 0 4 144 2
19: 0 49.00 52 16 0 C 0 10 7 0 2 51 3
20: 0 79.00 53 16 0 B 0 20 9 0 0 40 4
21: 0 68.00 45 1 0 A 0 370 245 0 298 1826 3
22: 0 1074.00 46 1 0 A 0 605 220 0 280 1421 7
23: 0 19.00 84 1 0 A 0 357 214 0 104 1273 3
24: 0 68.00 42 1 0 A 0 107 97 0 224 1526 3
25: 0 226.00 39 1 0 A 0 228 162 0 139 559 3
26: 0 49.00 92 16 0 C 0 4 3 0 0 0 3
27: 0 68.00 46 1 0 A 0 155 104 0 60 1170 3
28: 1 98.00 29 2 0 C 0 15 13 0 1 659 3
29: 0 248.00 44 1 0 A 0 347 204 0 281 1484 4
30: 0 19.00 84 1 0 A 0 302 166 0 170 2800 3
31: 0 444.00 20 16 0 A 0 569 411 1 369 1095 4
32: 0 157.00 20 16 0 B 0 38 30 0 18 265 3
33: 0 208.00 71 16 0 B 0 22 22 0 1 210 3
34: 1 84.00 27 13 0 A 0 37 24 0 1 649 1
35: 1 297.00 17 7 0 A 0 26 21 0 0 0 1
36: 1 49.00 43 16 1 C 0 4 4 0 0 0 2
37: 0 99.00 36 1 0 A 0 614 432 0 851 2839 4
38: 0 354.00 91 2 1 C 0 74 48 0 102 1005 9
39: 0 68.00 62 16 0 A 0 42 32 0 0 0 3
40: 0 49.00 78 16 0 C 0 12 10 0 0 95 3
41: 0 49.00 57 16 0 C 1 9 8 0 1 582 3
42: 0 68.00 49 1 0 A 0 64 47 0 49 112 3
43: 0 583.00 70 2 1 A 0 502 293 0 406 2734 9
44: 0 187.00 29 1 0 A 0 186 129 0 118 2746 5
45: 0 178.00 52 1 0 A 0 900 484 0 180 1701 4
46: 1 98.00 50 44 0 C 0 13 12 0 1 647 4
47: 1 548.00 21 14 0 A 0 19 14 0 0 0 1
48: 0 178.00 28 16 0 C 0 43 33 0 6 921 3
49: 1 49.00 20 20 0 C 0 8 6 0 0 0 1
50: 0 49.00 124 124 1 A 0 14 11 0 0 0 1
a b c d e f g h i j k l m
This data is not normalised, but it doesn't matter at this stage.
I can't get a simple gbm model work using the gbm package:
> require(gbm)
> gbm_model <- gbm(a ~ .
, data = train
, distribution="bernoulli"
, n.trees= 10
, shrinkage=0.001
, bag.fraction = 1
, train.fraction = 0.5
, n.minobsinnode = 3
, cv.folds = 0 # no cross-validation
, keep.data=TRUE
, verbose=TRUE
)
Iter TrainDeviance ValidDeviance StepSize Improve
1 nan nan 0.0010 nan
2 nan nan 0.0010 nan
3 nan nan 0.0010 nan
4 nan nan 0.0010 nan
5 nan nan 0.0010 nan
6 nan nan 0.0010 nan
7 nan nan 0.0010 nan
8 nan nan 0.0010 nan
9 nan nan 0.0010 nan
10 nan nan 0.0010 nan
Columns 'e' and 'f' are factors. Train data sample size is approximately 6,000. I've tried running gbm with various bag.fraction, train.fraction, n.tree, and shrinkage values but still get the same result of all NaNs. Trees and SVM work without any problem on the same data. I even tried converting column 'f' to character, as it was suggested in previous posts, and it didn't work.
Edit: data has no NAs or invalid values. I tried one-hot encoding the 'f' column and still same results.
In my case, this issue was resolved by converting the dependent variable to character.
gbm_model <- gbm(as.character(a) ~ .
, data = train
, distribution="bernoulli"
, n.trees= 10
, shrinkage=0.001
, bag.fraction = 1
, train.fraction = 0.5
, n.minobsinnode = 3
, cv.folds = 0 # no cross-validation
, keep.data=TRUE
, verbose=TRUE
)
I am using R's r2dtable function to generate contingency tables with given marginals. However, when inspecting the resulting tables values look somewhat too concentrated to the midpoints. Example:
set.seed(1)
matrices <- r2dtable(1e4, c(100, 100), c(100, 100))
vec.vals <- vapply(matrices, function(x) x[1, 1], numeric(1))
> table(vec.vals)
vec.vals
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
1 1 1 7 25 49 105 182 268 440 596 719 954 1072 1152 1048
52 53 54 55 56 57 58 59 60 61 62
1022 775 573 404 290 156 83 50 19 6 2
So the minimal upper left corner value is 36 and the max is 62 out of 10,000 simulations.
Is there a way to achieve somewhat less concentrated matrices?
You need to consider that it would be extremely unlikely that any given random draw would have a value with and upper left corner of 35. 1e4 attempts may not be sufficient to realize such an event. Look at the theoretic predictions (courtesy of P. Dalgaard on Rhelp list this morning.):
round(dhyper(0:100,100,100,100)*1e4)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[18] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[35] 0 0 0 1 4 9 21 45 88 160 269 417 596 787 959 1081 1124
[52] 1081 959 787 596 417 269 160 88 45 21 9 4 1 0 0 0 0
[69] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[86] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
If you increase the number of draws the probability of a single value of 1 "widens":
vec.vals <- vapply(matrices, function(x) x[1, 1], numeric(1)); table(vec.vals)
vec.vals
33 34 35 36 37 38 39 40 41 42 43 44 45
1 3 8 47 141 359 864 2148 4515 8946 15928 27013 41736
46 47 48 49 50 51 52 53 54 55 56 57 58
59558 78717 96153 108322 112524 107585 96042 78054 60019 41556 26848 16134 8627
59 60 61 62 63 64 65 66 68
4580 2092 933 351 138 42 11 4 1
... as predicted:
round(dhyper(0:100,100,100,100)*1e6)
[1] 0 0 0 0 0 0 0 0 0 0 0 0
[13] 0 0 0 0 0 0 0 0 0 0 0 0
[25] 0 0 0 0 0 0 0 0 0 1 4 13
[37] 43 129 355 897 2087 4469 8819 16045 26927 41700 59614 78694
[49] 95943 108050 112416 108050 95943 78694 59614 41700 26927 16045 8819 4469
[61] 2087 897 355 129 43 13 4 1 0 0 0 0
[73] 0 0 0 0 0 0 0 0 0 0 0 0
[85] 0 0 0 0 0 0 0 0 0 0 0 0
[97] 0 0 0 0 0
To get less concentrated matrices, you will have to find a balance between the number of columns / rows, totals and number of matrices. Consider the following sets:
m2rep <- r2dtable(1e4, rep(100,2), rep(100,2))
m2seq <- r2dtable(1e4, seq(50,100,50), seq(50,100,50))
which gives differences in number of unique value:
> length(unique(unlist(m2rep)))
[1] 29
> length(unique(unlist(m2seq)))
[1] 58
plotting this with:
par(mfrow = c(1,2))
plot(table(unlist(m2rep)))
plot(table(unlist(m2seq)))
gives:
Now consider:
m20rep <- r2dtable(1e4, rep(100,20), rep(100,20))
m20seq <- r2dtable(1e4, seq(50,1000,50), seq(50,1000,50))
which gives:
> length(unique(unlist(m20rep)))
[1] 20
> length(unique(unlist(m20seq)))
[1] 130
plotting this with:
par(mfrow = c(1,2))
plot(table(unlist(m20rep)))
plot(table(unlist(m20seq)))
gives:
As you can see, playing with the parameters helps.
HTH
I have to matrices (or data frames) that contain the same column and rownames.
The rownames of matrix 1 has an ID called dataTissue, the rownames of matrix 2 has the ID dataSerum. I would like to combine the two matrices where identical rows are placed adjacent (on top of each other). Please see my desired output.
I was thinking of using rbind, but I am not sure how to get this structure.
matrix 1:
> head(TumorTissue3)
020 045 080 082 084 086 088 090 091 092 094 096 1018 102 1065
dataTissue.hsa-let-7a-2-3p 1 0 1 0 0 0 1 1 0 2 0 0 0 5 0
dataTissue.hsa-let-7a-3p 2 0 0 0 1 0 1 1 0 2 1 1 1 1 0
dataTissue.hsa-let-7a-5p 67 12 25 34 40 115 42 33 26 58 22 149 64 178 52
dataTissue.hsa-let-7b-3p 11 5 10 15 1 34 29 59 16 30 11 44 11 65 3
dataTissue.hsa-let-7b-5p 4289 689 902 3340 3947 7326 3146 6249 2032 5664 1657 6619 1577 21132 720
dataTissue.hsa-let-7c-3p 1 0 0 2 0 9 2 13 2 10 2 13 5 9 0
1068 1104 112 113 1167 1196 120 121 1222 1237 1241 1302 1304 1322 134
dataTissue.hsa-let-7a-2-3p 2 11 0 0 0 0 3 0 2 1 0 0 3 5 1
dataTissue.hsa-let-7a-3p 0 0 1 0 0 1 0 0 1 4 1 0 2 3 0
dataTissue.hsa-let-7a-5p 70 266 60 8 29 99 90 37 102 93 28 22 156 214 176
dataTissue.hsa-let-7b-3p 14 15 24 12 8 8 43 25 14 33 9 12 16 38 11
dataTissue.hsa-let-7b-5p 1780 4185 5797 1168 1039 1006 10818 3269 2893 8847 3136 4990 1798 10142 3248
dataTissue.hsa-let-7c-3p 5 7 5 2 1 3 3 3 1 10 27 1 17 11 3
1372 140 145 146 1474 1532 1540 157 158 1588 1604 161 1743 176
dataTissue.hsa-let-7a-2-3p 0 0 0 0 6 1 10 0 6 1 0 1 0 2
dataTissue.hsa-let-7a-3p 1 0 1 0 0 1 0 0 3 0 0 2 0 1
dataTissue.hsa-let-7a-5p 18 1 53 17 129 54 110 2 165 70 51 165 81 77
dataTissue.hsa-let-7b-3p 3 0 22 12 46 3 60 0 79 9 15 40 3 50
dataTissue.hsa-let-7b-5p 931 245 3707 3632 16730 2653 13619 93 27568 3485 6202 18206 3094 11185
dataTissue.hsa-let-7c-3p 1 0 12 0 10 0 5 0 20 10 8 7 2 9
1808 1809 185 1859 186 1894 192 201 204 21 215 2218 236 27 32
dataTissue.hsa-let-7a-2-3p 2 1 1 0 1 0 0 0 3 0 2 6 0 3 5
dataTissue.hsa-let-7a-3p 0 0 0 0 1 0 0 0 1 0 3 3 0 1 0
dataTissue.hsa-let-7a-5p 33 160 56 16 92 63 90 3 119 58 116 46 37 137 40
dataTissue.hsa-let-7b-3p 11 1 23 10 18 3 48 14 34 16 54 23 12 96 33
dataTissue.hsa-let-7b-5p 3497 548 5575 2886 6664 1030 5895 604 8151 4076 14150 11132 2154 24793 5654
dataTissue.hsa-let-7c-3p 3 3 5 6 6 2 4 2 6 9 20 6 2 11 6
38 39 45 46 bf33 d10 HEP014 HEP015 mm7 s26 TxHEP-014 TxHEP-015
dataTissue.hsa-let-7a-2-3p 0 0 6 0 0 0 1 2 2 4 0 1
dataTissue.hsa-let-7a-3p 0 0 0 2 0 2 2 0 1 2 0 1
dataTissue.hsa-let-7a-5p 18 75 192 41 41 88 55 119 24 112 223 25
dataTissue.hsa-let-7b-3p 6 16 56 11 17 24 8 29 12 29 7 18
dataTissue.hsa-let-7b-5p 1648 2805 19275 1769 4554 5316 1552 7605 2369 7495 33820 2144
dataTissue.hsa-let-7c-3p 3 1 14 2 2 3 6 18 1 23 3 2
TxHEP-018 vs29
dataTissue.hsa-let-7a-2-3p 1 1
dataTissue.hsa-let-7a-3p 0 0
dataTissue.hsa-let-7a-5p 13 50
dataTissue.hsa-let-7b-3p 23 47
dataTissue.hsa-let-7b-5p 1631 4990
dataTissue.hsa-let-7c-3p 1 11
matrix 2:
> head(Serum3)
020 045 080 082 084 086 088 090 091 092 094 096 1018 102 1065 1068
dataSerum.hsa-let-7a-2-3p 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
dataSerum.hsa-let-7a-3p 4 2 0 2 329 1 186 0 2 4 1 6 13 7 15 3
dataSerum.hsa-let-7a-5p 988 2033 587 1480 4035 1167 4641 761 668 4118 6040 2660 10368 5802 5668 2709
dataSerum.hsa-let-7b-3p 9 8 4 18 76 3 62 1 5 24 9 9 41 10 30 6
dataSerum.hsa-let-7b-5p 1499 849 108 868 3197 202 2411 273 224 1309 943 822 5819 1594 3335 1164
dataSerum.hsa-let-7c-3p 0 0 0 0 29 0 11 0 0 0 5 0 2 0 0 1
1104 112 113 1167 1196 120 121 1222 1237 1241 1302 1304 1322 134 1372
dataSerum.hsa-let-7a-2-3p 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
dataSerum.hsa-let-7a-3p 15 2 0 8 5 2 0 11 13 51 4 0 1 7 0
dataSerum.hsa-let-7a-5p 30222 1836 1518 3902 5122 4597 983 3809 6310 3165 4023 434 2489 1496 600
dataSerum.hsa-let-7b-3p 57 2 1 14 19 14 2 14 35 162 10 0 10 11 6
dataSerum.hsa-let-7b-5p 11314 329 354 2169 2277 747 256 1157 3328 3662 1057 274 1267 991 305
dataSerum.hsa-let-7c-3p 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0
140 145 146 1474 1532 1540 157 158 1588 1604 161 1743 176 1808 1809
dataSerum.hsa-let-7a-2-3p 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
dataSerum.hsa-let-7a-3p 27 10 0 2 2 5 0 40 0 0 19 0 1 4 4
dataSerum.hsa-let-7a-5p 5364 12731 670 473 1045 767 927 49689 535 8 78671 757 1502 1146 539
dataSerum.hsa-let-7b-3p 63 37 1 3 14 5 10 59 1 0 56 6 3 12 6
dataSerum.hsa-let-7b-5p 2262 3209 88 363 759 459 309 13482 234 3 15113 1064 545 587 569
dataSerum.hsa-let-7c-3p 6 0 0 0 0 0 1 0 0 0 0 0 1 0 0
185 1859 186 1894 192 201 204 21 215 2218 236 27 32
dataSerum.hsa-let-7a-2-3p 0 0 0 0 0 3 0 0 0 0 0 0 0
dataSerum.hsa-let-7a-3p 16 0 101 3 7 346 10 1 93 0 305 6 12
dataSerum.hsa-let-7a-5p 42694 528 18730 498 3410 20484 11907 1031 474051 2185 299085 14576 9218
dataSerum.hsa-let-7b-3p 24 7 164 14 16 85 29 12 111 9 145 32 12
dataSerum.hsa-let-7b-5p 5454 216 4647 182 1149 8973 2645 147 72681 807 46354 4672 2375
dataSerum.hsa-let-7c-3p 0 0 9 0 0 32 0 0 0 0 3 1 0
38 39 45 46 bf33 d10 HEP014 HEP015 mm7 s26 TxHEP-014 TxHEP-015
dataSerum.hsa-let-7a-2-3p 0 0 0 1 0 0 0 0 0 0 0 0
dataSerum.hsa-let-7a-3p 9 17 6 119 27 0 1 5 0 10 7 1
dataSerum.hsa-let-7a-5p 2395 4382 8361 9747 6440 616 2981 5851 291 1386 3709 2494
dataSerum.hsa-let-7b-3p 18 28 24 104 33 6 12 24 5 36 2 11
dataSerum.hsa-let-7b-5p 690 1756 3425 3972 2330 136 1035 2152 235 638 555 1409
dataSerum.hsa-let-7c-3p 1 3 2 10 1 0 0 0 0 2 0 0
TxHEP-018 vs29
dataSerum.hsa-let-7a-2-3p 0 0
dataSerum.hsa-let-7a-3p 0 2
dataSerum.hsa-let-7a-5p 397 266
dataSerum.hsa-let-7b-3p 0 9
dataSerum.hsa-let-7b-5p 67 182
dataSerum.hsa-let-7c-3p 0 0
Output:
dataTissue.hsa-let-7a-2-3p 23 24 35 ....
dataSerum.hsa-let-7a-2-3p 42 535 54 ....
dataTissue.hsa-let-7a-3p 234 224 35 ....
dataSerum.hsa-let-7a-3p 2 33 54 ....
We can rbind both the matrices and then order by the rownames after removing the prefix part 'dataTissue./dataSerum.' from the row names using sub.
res <- rbind(TumorTissue3, Serum3)
nm1 <- sub('^[.]+\\.', '', row.names(res))
res[order(nm1),]