r how to subtract elements of different length matrices - r

I've got two dataframes that have same number of rows (22 rows) and different number of columns.
sim_10(22 rows, 15 columns):
2 0.577967 0.023869 0.021571 0.481754 0.61584 0 0 0 0 0 0.024057 0.014209 1 0.085784
8 0.0775 0.274113 2.7e-05 0.01215 0.009345 0 0 0 0 0 0.004092 0.00784 0 0
And how can I do it in easy way..
...
nm_10(22 rows, 8 columns)
11 0.926554 0.256966 0.859375 0 0.191011 0 0 0
2 0.858757 0.256966 0.21875 0 0.662921 0 0.845506 0.090909
..
the first column of two dataframes are same just in different order(names of cases). I need to find the matching row names in nm_10 and sm_10 and subtract every element of sm_10 in that row to the every element in the nm_10. example:
for '2' sm_nm_10:
2 (0.577967-0.858757=-0.28079) (0.577967-0.256966=) (0.577967-0.21875) ...(0.577967-0.090909=..)
(0.023869-0.858757=) (0.023869-0.256966=) (0.023869-0.21875) ...(0.023869-0.090909=..)
....
(0.085784-0.858757=) (0.085784-0.256966=) (0.085784-0.21875) ...(0.085784-0.090909=..)
and that for all data.
Check every row's first column, find matching row and do operation.
Is there any simple way to do it? I looked into sweep, apply but couldn't figure out how to use them. I keep getting errors referring to length etc. I decided to keep it simple and here is what I have :
s = numeric()
for (i in 1:nrow(sm_10))
{
for (jj in 1:nrow(nm_10))
{
for (j in 2:ncol(nm_10))
{
for (ii in 2:ncol(sm_10))
{
sm_10[i,]%in% nm_10[jj,]
s <- sm_10[,ii]-nm_10[,j]
}}}}
What is wrong here? Could anyone explain and suggest better?
UPDATE:
The end result I need is all rows 22 with the elements subtractions. that is 22 rows with (14*7 ) columns..

We can subset the larger dataset ("sim_10") by indexing the row names and column names of the subset dataset ("nm_10") and subtract the subset data (which has elements in corresponding row/column positions as "nm_10") from "nm_10".
sim_10[rownames(nm_10),colnames(nm_10)] - nm_10
data
set.seed(24)
sim_10 <- as.data.frame(matrix(sample(1:20, 22*15, replace=TRUE), ncol=15))
set.seed(42)
nm_10 <- as.data.frame(matrix(sample(1:40, 22*8, replace=TRUE), ncol=8))
set.seed(32)
colnames(nm_10) <- sample(colnames(sim_10), 8, replace=FALSE)
rownames(nm_10) <- sample(rownames(sim_10), 22, replace=FALSE)

I think the best solution here is to replicate the LHS by a sufficient multiplier such that it will then possess the desired output width, and then simply subtract the RHS from it. This will naturally be a vectorized subtraction and will cycle the RHS a sufficient number of times to fully cover the widened LHS. We must just take care to ensure that the pairing of elements is correct, which requires two things: (1) reorder the rows of the RHS such that the key values align with the LHS, and (2) replicate the LHS using the each parameter of rep(), rather than the times parameter:
df1 <- as.data.frame(cbind(sample(1:22),matrix(1:(22*14),22)));
df2 <- as.data.frame(cbind(sample(1:22),matrix(1:(22*7),22)));
df1;
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
## 1 22 1 23 45 67 89 111 133 155 177 199 221 243 265 287
## 2 20 2 24 46 68 90 112 134 156 178 200 222 244 266 288
## 3 13 3 25 47 69 91 113 135 157 179 201 223 245 267 289
## 4 12 4 26 48 70 92 114 136 158 180 202 224 246 268 290
## 5 16 5 27 49 71 93 115 137 159 181 203 225 247 269 291
## 6 7 6 28 50 72 94 116 138 160 182 204 226 248 270 292
## 7 1 7 29 51 73 95 117 139 161 183 205 227 249 271 293
## 8 2 8 30 52 74 96 118 140 162 184 206 228 250 272 294
## 9 9 9 31 53 75 97 119 141 163 185 207 229 251 273 295
## 10 14 10 32 54 76 98 120 142 164 186 208 230 252 274 296
## 11 4 11 33 55 77 99 121 143 165 187 209 231 253 275 297
## 12 21 12 34 56 78 100 122 144 166 188 210 232 254 276 298
## 13 15 13 35 57 79 101 123 145 167 189 211 233 255 277 299
## 14 10 14 36 58 80 102 124 146 168 190 212 234 256 278 300
## 15 8 15 37 59 81 103 125 147 169 191 213 235 257 279 301
## 16 6 16 38 60 82 104 126 148 170 192 214 236 258 280 302
## 17 19 17 39 61 83 105 127 149 171 193 215 237 259 281 303
## 18 3 18 40 62 84 106 128 150 172 194 216 238 260 282 304
## 19 5 19 41 63 85 107 129 151 173 195 217 239 261 283 305
## 20 18 20 42 64 86 108 130 152 174 196 218 240 262 284 306
## 21 17 21 43 65 87 109 131 153 175 197 219 241 263 285 307
## 22 11 22 44 66 88 110 132 154 176 198 220 242 264 286 308
df2;
## V1 V2 V3 V4 V5 V6 V7 V8
## 1 6 1 23 45 67 89 111 133
## 2 17 2 24 46 68 90 112 134
## 3 12 3 25 47 69 91 113 135
## 4 20 4 26 48 70 92 114 136
## 5 13 5 27 49 71 93 115 137
## 6 10 6 28 50 72 94 116 138
## 7 16 7 29 51 73 95 117 139
## 8 15 8 30 52 74 96 118 140
## 9 21 9 31 53 75 97 119 141
## 10 22 10 32 54 76 98 120 142
## 11 1 11 33 55 77 99 121 143
## 12 18 12 34 56 78 100 122 144
## 13 9 13 35 57 79 101 123 145
## 14 4 14 36 58 80 102 124 146
## 15 11 15 37 59 81 103 125 147
## 16 19 16 38 60 82 104 126 148
## 17 8 17 39 61 83 105 127 149
## 18 5 18 40 62 84 106 128 150
## 19 3 19 41 63 85 107 129 151
## 20 7 20 42 64 86 108 130 152
## 21 2 21 43 65 87 109 131 153
## 22 14 22 44 66 88 110 132 154
cbind(df1[,1],as.data.frame(rep(df1[,-1],each=ncol(df2)-1))-as.matrix(df2[match(df1[,1],df2[,1]),-1]));
## df1[, 1] V2 V2.1 V2.2 V2.3 V2.4 V2.5 V2.6 V3 V3.1 V3.2 V3.3 V3.4 V3.5 V3.6 V4 V4.1 V4.2 V4.3 V4.4 V4.5 V4.6 V5 V5.1 V5.2 V5.3 V5.4 V5.5 V5.6 V6 V6.1 V6.2 V6.3 V6.4 V6.5 V6.6 V7 V7.1 V7.2 V7.3 V7.4 V7.5 V7.6 V8 V8.1 V8.2 V8.3 V8.4 V8.5 V8.6 V9 V9.1 V9.2 V9.3 V9.4 V9.5 V9.6 V10 V10.1 V10.2 V10.3 V10.4 V10.5 V10.6 V11 V11.1 V11.2 V11.3 V11.4 V11.5 V11.6 V12 V12.1 V12.2 V12.3 V12.4 V12.5 V12.6 V13 V13.1 V13.2 V13.3 V13.4 V13.5 V13.6 V14 V14.1 V14.2 V14.3 V14.4 V14.5 V14.6 V15 V15.1 V15.2 V15.3 V15.4 V15.5 V15.6
## 1 22 -9 -31 -53 -75 -97 -119 -141 13 -9 -31 -53 -75 -97 -119 35 13 -9 -31 -53 -75 -97 57 35 13 -9 -31 -53 -75 79 57 35 13 -9 -31 -53 101 79 57 35 13 -9 -31 123 101 79 57 35 13 -9 145 123 101 79 57 35 13 167 145 123 101 79 57 35 189 167 145 123 101 79 57 211 189 167 145 123 101 79 233 211 189 167 145 123 101 255 233 211 189 167 145 123 277 255 233 211 189 167 145
## 2 20 -2 -24 -46 -68 -90 -112 -134 20 -2 -24 -46 -68 -90 -112 42 20 -2 -24 -46 -68 -90 64 42 20 -2 -24 -46 -68 86 64 42 20 -2 -24 -46 108 86 64 42 20 -2 -24 130 108 86 64 42 20 -2 152 130 108 86 64 42 20 174 152 130 108 86 64 42 196 174 152 130 108 86 64 218 196 174 152 130 108 86 240 218 196 174 152 130 108 262 240 218 196 174 152 130 284 262 240 218 196 174 152
## 3 13 -2 -24 -46 -68 -90 -112 -134 20 -2 -24 -46 -68 -90 -112 42 20 -2 -24 -46 -68 -90 64 42 20 -2 -24 -46 -68 86 64 42 20 -2 -24 -46 108 86 64 42 20 -2 -24 130 108 86 64 42 20 -2 152 130 108 86 64 42 20 174 152 130 108 86 64 42 196 174 152 130 108 86 64 218 196 174 152 130 108 86 240 218 196 174 152 130 108 262 240 218 196 174 152 130 284 262 240 218 196 174 152
## 4 12 1 -21 -43 -65 -87 -109 -131 23 1 -21 -43 -65 -87 -109 45 23 1 -21 -43 -65 -87 67 45 23 1 -21 -43 -65 89 67 45 23 1 -21 -43 111 89 67 45 23 1 -21 133 111 89 67 45 23 1 155 133 111 89 67 45 23 177 155 133 111 89 67 45 199 177 155 133 111 89 67 221 199 177 155 133 111 89 243 221 199 177 155 133 111 265 243 221 199 177 155 133 287 265 243 221 199 177 155
## 5 16 -2 -24 -46 -68 -90 -112 -134 20 -2 -24 -46 -68 -90 -112 42 20 -2 -24 -46 -68 -90 64 42 20 -2 -24 -46 -68 86 64 42 20 -2 -24 -46 108 86 64 42 20 -2 -24 130 108 86 64 42 20 -2 152 130 108 86 64 42 20 174 152 130 108 86 64 42 196 174 152 130 108 86 64 218 196 174 152 130 108 86 240 218 196 174 152 130 108 262 240 218 196 174 152 130 284 262 240 218 196 174 152
## 6 7 -14 -36 -58 -80 -102 -124 -146 8 -14 -36 -58 -80 -102 -124 30 8 -14 -36 -58 -80 -102 52 30 8 -14 -36 -58 -80 74 52 30 8 -14 -36 -58 96 74 52 30 8 -14 -36 118 96 74 52 30 8 -14 140 118 96 74 52 30 8 162 140 118 96 74 52 30 184 162 140 118 96 74 52 206 184 162 140 118 96 74 228 206 184 162 140 118 96 250 228 206 184 162 140 118 272 250 228 206 184 162 140
## 7 1 -4 -26 -48 -70 -92 -114 -136 18 -4 -26 -48 -70 -92 -114 40 18 -4 -26 -48 -70 -92 62 40 18 -4 -26 -48 -70 84 62 40 18 -4 -26 -48 106 84 62 40 18 -4 -26 128 106 84 62 40 18 -4 150 128 106 84 62 40 18 172 150 128 106 84 62 40 194 172 150 128 106 84 62 216 194 172 150 128 106 84 238 216 194 172 150 128 106 260 238 216 194 172 150 128 282 260 238 216 194 172 150
## 8 2 -13 -35 -57 -79 -101 -123 -145 9 -13 -35 -57 -79 -101 -123 31 9 -13 -35 -57 -79 -101 53 31 9 -13 -35 -57 -79 75 53 31 9 -13 -35 -57 97 75 53 31 9 -13 -35 119 97 75 53 31 9 -13 141 119 97 75 53 31 9 163 141 119 97 75 53 31 185 163 141 119 97 75 53 207 185 163 141 119 97 75 229 207 185 163 141 119 97 251 229 207 185 163 141 119 273 251 229 207 185 163 141
## 9 9 -4 -26 -48 -70 -92 -114 -136 18 -4 -26 -48 -70 -92 -114 40 18 -4 -26 -48 -70 -92 62 40 18 -4 -26 -48 -70 84 62 40 18 -4 -26 -48 106 84 62 40 18 -4 -26 128 106 84 62 40 18 -4 150 128 106 84 62 40 18 172 150 128 106 84 62 40 194 172 150 128 106 84 62 216 194 172 150 128 106 84 238 216 194 172 150 128 106 260 238 216 194 172 150 128 282 260 238 216 194 172 150
## 10 14 -12 -34 -56 -78 -100 -122 -144 10 -12 -34 -56 -78 -100 -122 32 10 -12 -34 -56 -78 -100 54 32 10 -12 -34 -56 -78 76 54 32 10 -12 -34 -56 98 76 54 32 10 -12 -34 120 98 76 54 32 10 -12 142 120 98 76 54 32 10 164 142 120 98 76 54 32 186 164 142 120 98 76 54 208 186 164 142 120 98 76 230 208 186 164 142 120 98 252 230 208 186 164 142 120 274 252 230 208 186 164 142
## 11 4 -3 -25 -47 -69 -91 -113 -135 19 -3 -25 -47 -69 -91 -113 41 19 -3 -25 -47 -69 -91 63 41 19 -3 -25 -47 -69 85 63 41 19 -3 -25 -47 107 85 63 41 19 -3 -25 129 107 85 63 41 19 -3 151 129 107 85 63 41 19 173 151 129 107 85 63 41 195 173 151 129 107 85 63 217 195 173 151 129 107 85 239 217 195 173 151 129 107 261 239 217 195 173 151 129 283 261 239 217 195 173 151
## 12 21 3 -19 -41 -63 -85 -107 -129 25 3 -19 -41 -63 -85 -107 47 25 3 -19 -41 -63 -85 69 47 25 3 -19 -41 -63 91 69 47 25 3 -19 -41 113 91 69 47 25 3 -19 135 113 91 69 47 25 3 157 135 113 91 69 47 25 179 157 135 113 91 69 47 201 179 157 135 113 91 69 223 201 179 157 135 113 91 245 223 201 179 157 135 113 267 245 223 201 179 157 135 289 267 245 223 201 179 157
## 13 15 5 -17 -39 -61 -83 -105 -127 27 5 -17 -39 -61 -83 -105 49 27 5 -17 -39 -61 -83 71 49 27 5 -17 -39 -61 93 71 49 27 5 -17 -39 115 93 71 49 27 5 -17 137 115 93 71 49 27 5 159 137 115 93 71 49 27 181 159 137 115 93 71 49 203 181 159 137 115 93 71 225 203 181 159 137 115 93 247 225 203 181 159 137 115 269 247 225 203 181 159 137 291 269 247 225 203 181 159
## 14 10 8 -14 -36 -58 -80 -102 -124 30 8 -14 -36 -58 -80 -102 52 30 8 -14 -36 -58 -80 74 52 30 8 -14 -36 -58 96 74 52 30 8 -14 -36 118 96 74 52 30 8 -14 140 118 96 74 52 30 8 162 140 118 96 74 52 30 184 162 140 118 96 74 52 206 184 162 140 118 96 74 228 206 184 162 140 118 96 250 228 206 184 162 140 118 272 250 228 206 184 162 140 294 272 250 228 206 184 162
## 15 8 -2 -24 -46 -68 -90 -112 -134 20 -2 -24 -46 -68 -90 -112 42 20 -2 -24 -46 -68 -90 64 42 20 -2 -24 -46 -68 86 64 42 20 -2 -24 -46 108 86 64 42 20 -2 -24 130 108 86 64 42 20 -2 152 130 108 86 64 42 20 174 152 130 108 86 64 42 196 174 152 130 108 86 64 218 196 174 152 130 108 86 240 218 196 174 152 130 108 262 240 218 196 174 152 130 284 262 240 218 196 174 152
## 16 6 15 -7 -29 -51 -73 -95 -117 37 15 -7 -29 -51 -73 -95 59 37 15 -7 -29 -51 -73 81 59 37 15 -7 -29 -51 103 81 59 37 15 -7 -29 125 103 81 59 37 15 -7 147 125 103 81 59 37 15 169 147 125 103 81 59 37 191 169 147 125 103 81 59 213 191 169 147 125 103 81 235 213 191 169 147 125 103 257 235 213 191 169 147 125 279 257 235 213 191 169 147 301 279 257 235 213 191 169
## 17 19 1 -21 -43 -65 -87 -109 -131 23 1 -21 -43 -65 -87 -109 45 23 1 -21 -43 -65 -87 67 45 23 1 -21 -43 -65 89 67 45 23 1 -21 -43 111 89 67 45 23 1 -21 133 111 89 67 45 23 1 155 133 111 89 67 45 23 177 155 133 111 89 67 45 199 177 155 133 111 89 67 221 199 177 155 133 111 89 243 221 199 177 155 133 111 265 243 221 199 177 155 133 287 265 243 221 199 177 155
## 18 3 -1 -23 -45 -67 -89 -111 -133 21 -1 -23 -45 -67 -89 -111 43 21 -1 -23 -45 -67 -89 65 43 21 -1 -23 -45 -67 87 65 43 21 -1 -23 -45 109 87 65 43 21 -1 -23 131 109 87 65 43 21 -1 153 131 109 87 65 43 21 175 153 131 109 87 65 43 197 175 153 131 109 87 65 219 197 175 153 131 109 87 241 219 197 175 153 131 109 263 241 219 197 175 153 131 285 263 241 219 197 175 153
## 19 5 1 -21 -43 -65 -87 -109 -131 23 1 -21 -43 -65 -87 -109 45 23 1 -21 -43 -65 -87 67 45 23 1 -21 -43 -65 89 67 45 23 1 -21 -43 111 89 67 45 23 1 -21 133 111 89 67 45 23 1 155 133 111 89 67 45 23 177 155 133 111 89 67 45 199 177 155 133 111 89 67 221 199 177 155 133 111 89 243 221 199 177 155 133 111 265 243 221 199 177 155 133 287 265 243 221 199 177 155
## 20 18 8 -14 -36 -58 -80 -102 -124 30 8 -14 -36 -58 -80 -102 52 30 8 -14 -36 -58 -80 74 52 30 8 -14 -36 -58 96 74 52 30 8 -14 -36 118 96 74 52 30 8 -14 140 118 96 74 52 30 8 162 140 118 96 74 52 30 184 162 140 118 96 74 52 206 184 162 140 118 96 74 228 206 184 162 140 118 96 250 228 206 184 162 140 118 272 250 228 206 184 162 140 294 272 250 228 206 184 162
## 21 17 19 -3 -25 -47 -69 -91 -113 41 19 -3 -25 -47 -69 -91 63 41 19 -3 -25 -47 -69 85 63 41 19 -3 -25 -47 107 85 63 41 19 -3 -25 129 107 85 63 41 19 -3 151 129 107 85 63 41 19 173 151 129 107 85 63 41 195 173 151 129 107 85 63 217 195 173 151 129 107 85 239 217 195 173 151 129 107 261 239 217 195 173 151 129 283 261 239 217 195 173 151 305 283 261 239 217 195 173
## 22 11 7 -15 -37 -59 -81 -103 -125 29 7 -15 -37 -59 -81 -103 51 29 7 -15 -37 -59 -81 73 51 29 7 -15 -37 -59 95 73 51 29 7 -15 -37 117 95 73 51 29 7 -15 139 117 95 73 51 29 7 161 139 117 95 73 51 29 183 161 139 117 95 73 51 205 183 161 139 117 95 73 227 205 183 161 139 117 95 249 227 205 183 161 139 117 271 249 227 205 183 161 139 293 271 249 227 205 183 161
For a demo that's easier to verify by eye, here I'll use three rows, five data columns on the LHS, and two data columns on the RHS:
df1 <- as.data.frame(cbind(sample(1:3),matrix(1:(3*5),3)));
df2 <- as.data.frame(cbind(sample(1:3),matrix(1:(3*2),3)));
df1;
## V1 V2 V3 V4 V5 V6
## 1 3 1 4 7 10 13
## 2 1 2 5 8 11 14
## 3 2 3 6 9 12 15
df2;
## V1 V2 V3
## 1 3 1 4
## 2 2 2 5
## 3 1 3 6
cbind(df1[,1],as.data.frame(rep(df1[,-1],each=ncol(df2)-1))-as.matrix(df2[match(df1[,1],df2[,1]),-1]));
## df1[, 1] V2 V2.1 V3 V3.1 V4 V4.1 V5 V5.1 V6 V6.1
## 1 3 0 -3 3 0 6 3 9 6 12 9
## 2 1 -1 -4 2 -1 5 2 8 5 11 8
## 3 2 1 -2 4 1 7 4 10 7 13 10
Notes:
Since the subtraction step must exclude the key column, rep() must operate on df1[,-1]. The -1 column subscript excludes the key column, which is assumed to be the first column in the data.frame.
The argument to each must be the number of subtrahends for each minuend, which means it also must exclude the key by subtracting one from ncol(df2).
Technically, when given a data.frame, rep() operates component-wise on the underlying list. But this works out for our purposes, because we can coerce back to data.frame with a call to as.data.frame(), and it's as if each individual element was replicated horizontally within its row. We are then ready with the widened data.frame to serve as the LHS of the subtraction.
In order to reorder the rows of the RHS, we first need to derive the correct row order. This can be done with match(df1[,1],df2[,1]). This basically says "for each key value in df1 in the order they occur in df1, return the row index in which that key value can be found in df2." The resulting index vector can then be used to row-index df2 to order it to align with df1. In the same index operation, we can exclude the key column of df2, fully preparing it for the cyclic subtraction, thus we have df2[match(df1[,1],df2[,1]),-1].
Unfortunately, it is not possible to subtract data.frames from each other, unless they are identical in size (otherwise you get the error ‘-’ only defined for equally-sized data frames). Thus I had to add an as.matrix() call on the RHS before subtracting. Another possible solution here could be to replicate the RHS to match the size of the LHS.
The key column had to be "restored" after the subtraction, hence the cbind() call wrapping the subtraction, which prepends the key column from df1 (df1[,1]).
I didn't bother to set any column names, since you haven't specified a requirement for them in your question. You can set them afterward if necessary via names()/setNames()/colnames()/dimnames().

Related

How do I retrieve an element along with the list in which it resides from a list of lists?

Suppose, I am having a list of lists like below:
> myList
[[1]]
[1] 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217
[[2]]
[1] 1 8 15 22 29 36 43 50 57 64 71 78 85 92 99 106 113 120 127 134 141 148 155 162 169 176 183 190 197 204 211 218
[[3]]
[1] 2 9 16 23 30 37 44 51 58 65 72 79 86 93 100 107 114 121 128 135 142 149 156 163 170 177 184 191 198 205 212 219
[[4]]
[1] 3 10 17 24 31 38 45 52 59 66 73 80 87 94 101 108 115 122 129 136 143 150 157 164 171 178 185 192 199 206 213 220
[[5]]
[1] 4 11 18 25 32 39 46 53 60 67 74 81 88 95 102 109 116 123 130 137 144 151 158 165 172 179 186 193 200 207 214 221
How do I search for an element in this list of lists and retrieve the entire list in which it belongs?
I tried something like below:
> myList[grep(7, myList)][[1]]
[1] 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217
This case looks correct, but when I tried this for the below case, I got the wrong result.
> myList[grep(18, myList)][[1]]
[1] 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217
while the correct output should be :
[1] 4 11 18 25 32 39 46 53 60 67 74 81 88 95 102 109 116 123 130 137 144 151 158 165 172 179 186 193 200 207 214 221
Is there any possible solution to this?
EDIT::
The sample list can be produced using --
l <- seq(0, 194)
myList <- list()
for (d in l){
temp <- intersect(seq(d, max(l), by = 7),l)
if (any(sapply(myList,function(x) d %in% x)) == FALSE){
myList <- append(myList, list(temp))
}
}
Could try:
myList[sapply(myList, function(x) any(x %in% 7))]
Use purrr package:
library(purrr)
keep(mylist, function(x, y) {any(x == y)}, y = 18)
purrr provides many useful list-handling functions which are documented in a cheatsheet that can be found here
If 18 is a number you wish to find in the list, try:
myList[sapply(myList, function(x) 18 %in% x)]

SVG DataURI in CSS not working in Firefox

body {
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='30.000000pt' height='30.000000pt' viewBox='0 0 300.000000 300.000000' preserveAspectRatio='xMidYMid meet'><g transform='translate(0.000000,300.000000) scale(0.100000,-0.100000)' fill='#3F0607' stroke='none'><path class='node' id='node1' d='M1255 2979 c-214 -37 -373 -98 -560 -212 -105 -64 -246 -187 -333 -291 -457 -540 -483 -1307 -65 -1871 453 -611 1297 -784 1958 -402 156 91 335 250 448 399 31 42 57 80 57 84 0 4 6 15 14 23 30 34 105 191 140 293 124 359 113 724 -31 1078 -155 380 -487 695 -874 831 -246 86 -514 110 -754 68z m464 -544 c153 -37 268 -120 323 -233 31 -62 33 -73 33 -172 0 -82 -4 -115 -19 -150 -49 -111 -167 -242 -326 -359 -179 -132 -213 -178 -227 -302 l-8 -74 -55 0 -55 0 2 107 c3 151 25 219 118 363 114 178 161 315 151 445 -9 133 -48 204 -134 246 -47 24 -61 26 -135 22 -63 -3 -92 -10 -119 -26 -55 -34 -53 -51 12 -120 68 -72 85 -113 77 -190 -11 -108 -86 -165 -215 -164 -64 1 -82 5 -121 29 -134 83 -137 294 -8 427 84 85 184 138 312 162 88 17 306 11 394 -11z m-160 -1453 c25 -15 61 -49 80 -76 33 -46 36 -55 36 -125 0 -66 -4 -82 -27 -117 -108 -167 -308 -166 -417 1 -22 34 -26 51 -26 115 1 79 11 104 64 163 72 79 197 96 290 39z'></path></g><g transform='translate(0.000000,300.000000) scale(0.100000,-0.100000)' fill='#FFFFFF' stroke='none'><path class='node' id='node4' d='M1325 2446 c-128 -24 -228 -77 -312 -162 -129 -133 -126 -344 8 -427 39 -24 57 -28 121 -29 129 -1 204 56 215 164 8 77 -9 118 -77 190 -65 69 -67 86 -12 120 27 16 56 23 119 26 74 4 88 2 135 -22 86 -42 125 -113 134 -246 10 -130 -37 -267 -151 -445 -93 -144 -115 -212 -118 -363 l-2 -107 55 0 55 0 8 74 c14 124 48 170 227 302 159 117 277 248 326 359 15 35 19 68 19 150 0 99 -2 110 -33 172 -55 113 -170 196 -323 233 -88 22 -306 28 -394 11z'></path><path class='node' id='node7' d='M1385 1011 c-67 -17 -116 -56 -160 -126 -14 -22 -19 -49 -20 -105 0 -64 4 -81 26 -115 109 -167 309 -168 417 -1 23 35 27 51 27 117 0 70 -3 79 -36 125 -61 85 -165 128 -254 105z'></path></g></svg>") 50% 50% repeat transparent;
}
Please find the inline SVG in the CSS. This doesn't show anything in the background. not getting any issue as well. Please let me know if this is the right way of creating the background ? please note I am using Firefox. Same works in Chrome.
Firefox doesn't like the # in your data URL. Replace all the hashes with %23.
body {
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='30.000000pt' height='30.000000pt' viewBox='0 0 300.000000 300.000000' preserveAspectRatio='xMidYMid meet'><g transform='translate(0.000000,300.000000) scale(0.100000,-0.100000)' fill='%233F0607' stroke='none'><path class='node' id='node1' d='M1255 2979 c-214 -37 -373 -98 -560 -212 -105 -64 -246 -187 -333 -291 -457 -540 -483 -1307 -65 -1871 453 -611 1297 -784 1958 -402 156 91 335 250 448 399 31 42 57 80 57 84 0 4 6 15 14 23 30 34 105 191 140 293 124 359 113 724 -31 1078 -155 380 -487 695 -874 831 -246 86 -514 110 -754 68z m464 -544 c153 -37 268 -120 323 -233 31 -62 33 -73 33 -172 0 -82 -4 -115 -19 -150 -49 -111 -167 -242 -326 -359 -179 -132 -213 -178 -227 -302 l-8 -74 -55 0 -55 0 2 107 c3 151 25 219 118 363 114 178 161 315 151 445 -9 133 -48 204 -134 246 -47 24 -61 26 -135 22 -63 -3 -92 -10 -119 -26 -55 -34 -53 -51 12 -120 68 -72 85 -113 77 -190 -11 -108 -86 -165 -215 -164 -64 1 -82 5 -121 29 -134 83 -137 294 -8 427 84 85 184 138 312 162 88 17 306 11 394 -11z m-160 -1453 c25 -15 61 -49 80 -76 33 -46 36 -55 36 -125 0 -66 -4 -82 -27 -117 -108 -167 -308 -166 -417 1 -22 34 -26 51 -26 115 1 79 11 104 64 163 72 79 197 96 290 39z'></path></g><g transform='translate(0.000000,300.000000) scale(0.100000,-0.100000)' fill='%23FFFFFF' stroke='none'><path class='node' id='node4' d='M1325 2446 c-128 -24 -228 -77 -312 -162 -129 -133 -126 -344 8 -427 39 -24 57 -28 121 -29 129 -1 204 56 215 164 8 77 -9 118 -77 190 -65 69 -67 86 -12 120 27 16 56 23 119 26 74 4 88 2 135 -22 86 -42 125 -113 134 -246 10 -130 -37 -267 -151 -445 -93 -144 -115 -212 -118 -363 l-2 -107 55 0 55 0 8 74 c14 124 48 170 227 302 159 117 277 248 326 359 15 35 19 68 19 150 0 99 -2 110 -33 172 -55 113 -170 196 -323 233 -88 22 -306 28 -394 11z'></path><path class='node' id='node7' d='M1385 1011 c-67 -17 -116 -56 -160 -126 -14 -22 -19 -49 -20 -105 0 -64 4 -81 26 -115 109 -167 309 -168 417 -1 23 35 27 51 27 117 0 70 -3 79 -36 125 -61 85 -165 128 -254 105z'></path></g></svg>") 50% 50% repeat transparent;
}
When you use background-image reference the svg as a file with a a path.
background-image: url(http://www.domain.com/path/img.svg);
So put all of that code that makes the svg into a text file, then change the extension from .txt to .svg
If you want to use it inline convert your svg with this tool: http://www.mobilefish.com/services/base64/base64.php
.imgSVG {
background: url("data:image/svg+xml;base64,[data]");
}
[data] portion is where you place whatever you get from the online tool.
SVG: https://css-tricks.com/using-svg/
URI: https://css-tricks.com/data-uris/

continuous value supplied to discrete scale

I am new to ggplot2. In fact, I only discovered it last week and I haven't quite figured out yet how to use aesthetics and scales etc. There is probably a very easy solution to my problem but I couldn't find a satisfying answer online.
Sorry for the size of the message, but all the data used is in the following script:
dados
Fres Vc Lu
1 466 30 10
2 416 30 10
3 465 30 10
4 416 30 10
5 464 30 10
6 416 30 10
7 476 30 10
8 412 30 10
9 468 30 10
10 410 30 10
11 470 30 10
12 407 30 10
13 468 30 10
14 412 30 10
15 469 30 10
16 414 30 10
17 469 30 10
18 412 30 10
19 467 30 10
20 409 30 10
21 469 30 10
22 415 30 10
23 471 30 10
24 420 30 10
25 469 30 10
26 416 30 10
27 464 30 10
28 409 30 10
29 465 30 10
30 412 30 10
31 464 30 10
32 409 30 10
33 466 30 10
34 417 30 10
35 466 30 10
36 417 30 10
37 464 30 10
38 414 30 10
39 466 30 10
40 415 30 10
41 585 30 94
42 234 30 94
43 589 30 94
44 231 30 94
45 585 30 94
46 223 30 94
47 586 30 94
48 223 30 94
49 572 30 94
50 233 30 94
51 585 30 94
52 233 30 94
53 589 30 94
54 234 30 94
55 598 30 94
56 237 30 94
57 605 30 94
58 237 30 94
59 586 30 94
60 233 30 94
61 588 30 94
62 227 30 94
63 585 30 94
64 230 30 94
65 586 30 94
66 230 30 94
67 591 30 94
68 237 30 94
69 586 30 94
70 234 30 94
71 592 30 94
72 237 30 94
73 595 30 94
74 236 30 94
75 600 30 94
76 227 30 94
77 592 30 94
78 237 30 94
79 592 30 94
80 240 30 94
81 468 30 10
82 408 30 10
83 471 30 10
84 405 30 10
85 475 30 10
86 403 30 10
87 470 30 10
88 409 30 10
89 478 30 10
90 405 30 10
91 474 30 10
92 403 30 10
93 472 30 10
94 402 30 10
95 478 30 10
96 408 30 10
97 477 30 10
98 406 30 10
99 473 30 10
100 406 30 10
101 474 30 10
102 406 30 10
103 477 30 10
104 411 30 10
105 480 30 10
106 413 30 10
107 479 30 10
108 408 30 10
109 476 30 10
110 406 30 10
111 476 30 10
112 404 30 10
113 472 30 10
114 407 30 10
115 474 30 10
116 411 30 10
117 473 30 10
118 415 30 10
119 479 30 10
120 409 30 10
121 578 30 94
122 370 30 94
123 570 30 94
124 378 30 94
125 575 30 94
126 367 30 94
127 579 30 94
128 371 30 94
129 576 30 94
130 362 30 94
131 579 30 94
132 372 30 94
133 588 30 94
134 375 30 94
135 586 30 94
136 372 30 94
137 589 30 94
138 378 30 94
139 587 30 94
140 375 30 94
141 578 30 94
142 368 30 94
143 575 30 94
144 375 30 94
145 574 30 94
146 376 30 94
147 575 30 94
148 367 30 94
149 580 30 94
150 382 30 94
151 583 30 94
152 368 30 94
153 591 30 94
154 386 30 94
155 595 30 94
156 379 30 94
157 593 30 94
158 384 30 94
159 607 30 94
160 399 30 94
161 760 30 122
162 625 30 122
163 746 30 122
164 612 30 122
165 762 30 122
166 625 30 122
167 783 30 122
168 637 30 122
169 778 30 122
170 640 30 122
171 778 30 122
172 638 30 122
173 791 30 122
174 638 30 122
175 782 30 122
176 635 30 122
177 792 30 122
178 640 30 122
179 783 30 122
180 637 30 122
181 774 30 122
182 622 30 122
183 777 30 122
184 618 30 122
185 777 30 122
186 622 30 122
187 765 30 122
188 623 30 122
189 769 30 122
190 625 30 122
191 775 30 122
192 622 30 122
193 777 30 122
194 628 30 122
195 769 30 122
196 620 30 122
197 778 30 122
198 623 30 122
199 788 30 122
200 634 30 122
201 457 40 38
202 416 40 38
203 460 40 38
204 438 40 38
205 465 40 38
206 441 40 38
207 467 40 38
208 442 40 38
209 473 40 38
210 452 40 38
211 469 40 38
212 446 40 38
213 478 40 38
214 450 40 38
215 476 40 38
216 454 40 38
217 479 40 38
218 452 40 38
219 480 40 38
220 450 40 38
221 481 40 38
222 443 40 38
223 476 40 38
224 447 40 38
225 472 40 38
226 450 40 38
227 479 40 38
228 449 40 38
229 478 40 38
230 455 40 38
231 478 40 38
232 457 40 38
233 481 40 38
234 447 40 38
235 504 40 38
236 452 40 38
237 472 40 38
238 447 40 38
239 472 40 38
240 451 40 38
241 622 40 66
242 377 40 66
243 619 40 66
244 378 40 66
245 622 40 66
246 369 40 66
247 616 40 66
248 374 40 66
249 619 40 66
250 374 40 66
251 616 40 66
252 374 40 66
253 621 40 66
254 375 40 66
255 618 40 66
256 397 40 66
257 633 40 66
258 406 40 66
259 652 40 66
260 412 40 66
261 652 40 66
262 419 40 66
263 658 40 66
264 423 40 66
265 659 40 66
266 409 40 66
267 650 40 66
268 405 40 66
269 653 40 66
270 405 40 66
271 652 40 66
272 403 40 66
273 656 40 66
274 408 40 66
275 644 40 66
276 406 40 66
277 649 40 66
278 412 40 66
279 650 40 66
280 406 40 66
281 853 40 122
282 330 40 122
283 859 40 122
284 323 40 122
285 842 40 122
286 308 40 122
287 842 40 122
288 324 40 122
289 831 40 122
290 334 40 122
291 838 40 122
292 341 40 122
293 836 40 122
294 328 40 122
295 840 40 122
296 324 40 122
297 836 40 122
298 321 40 122
299 831 40 122
300 328 40 122
301 833 40 122
302 328 40 122
303 840 40 122
304 330 40 122
305 831 40 122
306 321 40 122
307 833 40 122
308 328 40 122
309 833 40 122
310 321 40 122
311 840 40 122
312 319 40 122
313 838 40 122
314 317 40 122
315 831 40 122
316 319 40 122
317 827 40 122
318 323 40 122
319 836 40 122
320 328 40 122
321 442 40 38
322 407 40 38
323 437 40 38
324 410 40 38
325 444 40 38
326 412 40 38
327 440 40 38
328 414 40 38
329 439 40 38
330 413 40 38
331 436 40 38
332 416 40 38
333 446 40 38
334 412 40 38
335 438 40 38
336 414 40 38
337 443 40 38
338 408 40 38
339 446 40 38
340 407 40 38
341 445 40 38
342 413 40 38
343 453 40 38
344 414 40 38
345 449 40 38
346 417 40 38
347 447 40 38
348 411 40 38
349 443 40 38
350 417 40 38
351 447 40 38
352 410 40 38
353 449 40 38
354 409 40 38
355 442 40 38
356 413 40 38
357 451 40 38
358 412 40 38
359 447 40 38
360 420 40 38
361 526 40 66
362 467 40 66
363 532 40 66
364 470 40 66
365 528 40 66
366 474 40 66
367 529 40 66
368 472 40 66
369 533 40 66
370 480 40 66
371 542 40 66
372 487 40 66
373 545 40 66
374 504 40 66
375 549 40 66
376 507 40 66
377 546 40 66
378 517 40 66
379 541 40 66
380 518 40 66
381 554 40 66
382 514 40 66
383 564 40 66
384 514 40 66
385 571 40 66
386 522 40 66
387 575 40 66
388 525 40 66
389 582 40 66
390 533 40 66
391 588 40 66
392 536 40 66
393 591 40 66
394 553 40 66
395 592 40 66
396 557 40 66
397 592 40 66
398 563 40 66
399 583 40 66
400 568 40 66
> dadosc <- summarySE(dados, measurevar="Fres", groupvars=c("Vc","Lu"))
> dadosc
Vc Lu N Fres sd se ci
1 30 10 80 440.6875 30.91540 3.456447 6.879885
2 30 94 80 445.0250 150.97028 16.878990 33.596789
3 30 122 40 701.7000 75.06688 11.869115 24.007552
4 40 38 80 444.6125 23.31973 2.607225 5.189552
5 40 66 80 526.7125 90.77824 10.149316 20.201707
6 40 122 40 581.1250 259.74092 41.068645 83.069175
> ggplot(dadosc, aes(x=Lu, y=Fres, colour=Vc)) +
+ geom_errorbar(aes(ymin=Fres-se, ymax=Fres+se), width=5) +
+ geom_point()
> pd <- position_dodge(0.1)
Up to here I got this graph, very close to my desired graph, except for the fact I´d like a legend with only two colors, one for Vc=30 and other for Vc=40.
![enter image description here][1]
Then I try the following script:
ggplot(dadosc, aes(x=Lu, y=Fres, ymax = max(Fres), colour=Vc, group=Vc)) +
+ geom_errorbar(aes(ymin=Fres-se, ymax=Fres+se), colour="black", width=.1, position=pd) +
+ geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
+ xlab("Machining lenght (mm)") +
+ ylab("Machining forces (N)") +
+ scale_colour_hue(name="Cutting Velocity",
+ breaks=c("30", "40"),
+ labels=c("Vc = 30 m/min", " Vc = 40 m/min "),
+ l=40) +
+ ggtitle("The Effect of Cutting Velocity on Machining Forces") +
+ expand_limits(y=0) +
+ scale_y_continuous(breaks=0:750*50) +
+ theme_bw() +
+ theme(legend.justification=c(1,0),
+ legend.position=c(1,0))
Error: Continuous value supplied to discrete scale
And I receive this message:
"Error: Continuous value supplied to discrete scale"!
Vc should be a factor if you want two values in the legend. You were getting that error because you were trying to scale Vc as discrete (breaks = c(30, 40)) when it was of type integer
ggplot(dadosc, aes(x=Lu, y=Fres, colour=factor(Vc))) +
...

R: number in a txt file split up by line

I have a problem in reading a .txt in to R.
The data is something like this:
68 89 103 1
37 8 103 9
78 93 8 12
3 50
I used readLine() in R and came up with a list. But when I compare it to the raw data, I find that , for example, the last "1" in the first line is not 1, it should be connected to the second line, which make the number to e 137, instead of 1 and 37. I think this data is split by " ". If I use readLine(), I manually split up the lines. How could I correctly read it?
And, number 9 is not connect to 78 since at the beginning of line 3, there is a space. number 12 is connected with 3 to form 123, since there is no space before 3.
Thanks. I even don't know how to search my problem in Google. Don't know how to express it.
182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63
102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91
1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1
63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1
37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134
134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9
1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123
137 161 179 182 140 152 182 182 81 63 88 134 84 134 182
7 11 9 2 9 4 6 7 6 1 13 2 1 10 4 5 11 11 9 12 1 3 1 3 3
Basically, what I am doing now is:
For example, the vector:
ind <- c(7, 11, 9, 2 ,9 ,4 ,6, 7, 6 ,1, 13, 2 ,1 ,10 ,4 ,5 ,11 ,11, 9 ,12, 1, 3 ,1, 3 ,3)
indicates that the block of number above should be split up according to the length specified by the vector. I know I can split up a vector by
split(vector, rep(1:length(ind), ind))
However, the problem is I can't read the block of number correctly.
Based on the conditions you described, i.e. if there is a space at the beginning of line after you read the file with readLines, then the last number in the previous line should be joined with the first number of the current line.
Using your second example (I didn't understand the ind though)
lines1 <- readLines(n=10)
182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63
102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91
1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1
63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1
37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134
134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9
1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123
137 161 179 182 140 152 182 182 81 63 88 134 84 134 182
lines2 <- lines1[lines1!=''] #remove blank lines
indx <- grep("^ ", lines2) #create a numeric index for lines that start with a space
indx1 <- indx-1 #index that is one above the previous `indx`
lines2[indx1] <- paste0(lines2[indx1], gsub("^\\s+", "", lines2[indx])) #paste the lines together using the two indexes
lines3 <- lines2[-indx] #remove the lines that belong to the first index
lines3
#[1] "182 63 68 152 130 134 145 152 98 152 182 88 95 105 130 137 167 152 81 71 84 126 134 152 116 130 91 63 68 84 95 152 105 152 63102 152 63 77 112 140 77 119 152 161 167 105 112 145 161 182 152 81 95 84 91 102 108 130 134 91"
#[2] "1 2 1 4 3 6 1 1 5 2 1 5 2 3 4 5 5 1 2 6 1"
#[3] "63 102 119 161 161 172 179 88 91 95 105 112 119 119 137 145 167 172 91 98 108 112 134 137 161 161 179 71 174 95 105 134 134 1"
#[4] "37 140 145 150 150 68 68 130 137 77 95 112 137 161 174 81 84 126 134 161 161 174 68 77 98 102 102 102 112 88 88 91 98 112 134134 137 137 140 140 152 152 77 179 112 71 71 74 77 112 116 116 140 140 167 77 95 126 150 88 126 130 130 134 63 74 84 84 88 9"
#[5] "1 95 108 134 137 179 81 88 105 116 123 140 145 152 161 161 179 88 95 112 119 126 126 150 157 179 68 68 84 102 105 119 123 123137 161 179 182 140 152 182 182 81 63 88 134 84 134 182"

Drawing heatmap in R; how can I restrict the amount of colors

I have the following numeric dataframe:
> cnv_n
V1 V2 V3 V4 V5 V6 V7 V8 V9
[1,] 31 37 30 30 33 30 33 36 44
[2,] 104 107 94 109 108 105 99 101 123
[3,] 522 545 539 554 500 537 454 474 512
[4,] 36 39 34 37 33 38 36 37 40
[5,] 62 70 62 64 62 60 57 61 70
[6,] 12 13 8 8 10 9 13 12 14
[7,] 70 75 62 66 70 70 74 79 80
[8,] 22 20 22 21 23 24 20 25 25
[9,] 75 76 77 74 73 69 55 67 77
[10,] 40 46 39 43 38 39 38 38 41
[11,] 171 186 172 171 164 169 157 175 190
[12,] 47 56 51 55 49 45 55 46 53
[13,] 183 200 170 191 172 168 195 207 210
[14,] 175 165 161 187 174 199 152 166 180
[15,] 5 7 5 8 8 6 8 7 6
[16,] 71 80 69 78 70 76 66 78 83
[17,] 377 413 369 401 371 366 360 362 415
[18,] 191 187 187 199 184 181 195 192 221
[19,] 294 321 273 306 289 273 298 326 340
[20,] 30 31 32 29 29 28 22 24 30
[21,] 109 123 108 114 106 105 105 111 123
[22,] 446 461 441 474 433 440 408 443 489
[23,] 498 537 467 511 490 479 429 496 552
[24,] 354 401 333 371 367 370 348 385 423
[25,] 319 338 299 322 289 297 263 325 350
I need to draw a heatmap, and this can be done as simply as:
> heatmap(cnv_n)
I have a few questions about what this does.
The dendogram rearranges the rows and columns together based on similarity to each other, correct?
If for some reason I do not want that done, I can do this:
heatmap(cnv_n, Rowv=NA, Colv=NA)
So now the order is preserved.
I am interested to know what are the properties of this heatmap. It obviously determined automatically what ranges need to be assigned what color. How can I obtain a legend as to what color represents what number range in my dataframe?
What if I decide that the ranges R guessed are to lenient or too stringent and in fact all I want is a maximum of 10 colors?
I read the info for heatmap but I am not advanced enough to understand how to find out this info.
Thanks for your help.
Adrian

Resources