Change SVG color in Vue [duplicate] - css

This question already has answers here:
img src SVG changing the styles with CSS
(26 answers)
Closed 11 months ago.
I have this static existing svg that I have difficulties changing the color.
<img class="icon-shop" src="#/assets/icon-shop.svg"/>
<style>
.icon-shop {
width: 32px;
fill: orange;
stroke: orange;
}
</style>
I've found a solution using filter attribute but it shouldn't be this complicated.
filter: invert(35%) sepia(36%) saturate(7009%) hue-rotate(2deg)
brightness(104%) contrast(88%);

Sample is this if you change the fill="YOUR COLOR" that should change. You can view any svg on any text editor and change from there.
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="300.000000pt" height="300.000000pt" viewBox="0 0 300.000000 300.000000"
preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.10, written by Peter Selinger 2001-2011
</metadata>
<g transform="translate(0.000000,300.000000) scale(0.100000,-0.100000)"
fill="red" stroke="none">
<path d="M212 2613 c-128 -45 -197 -230 -142 -376 26 -70 122 -200 188 -255
39 -33 219 -160 962 -678 41 -28 104 -67 140 -85 58 -30 74 -34 145 -34 74 0
85 3 150 39 39 21 165 104 280 185 116 81 341 239 502 350 217 151 310 221
361 275 108 114 152 208 153 328 1 119 -63 215 -171 253 -37 13 -209 15 -1284
14 -1087 0 -1246 -2 -1284 -16z"/>
<path d="M50 1228 c0 -745 -2 -723 72 -798 22 -23 58 -50 81 -61 40 -18 87
-19 1297 -19 l1255 0 50 24 c55 25 105 76 132 134 16 34 18 95 21 705 2 367 0
667 -3 667 -3 0 -34 -24 -69 -54 -36 -30 -106 -83 -158 -117 -160 -107 -596
-411 -753 -523 -304 -219 -447 -257 -656 -175 -88 36 -129 62 -546 355 -142
99 -337 234 -433 300 -96 65 -200 143 -232 173 l-58 53 0 -664z"/>
</g>
</svg>

Related

Animation css svg pump

Could someone help me with the amination of an svg?
I am trying to create an animation of the pump impeller. What I want is for the last 4 elements of svg to do a rotating impeller effect. Make it look like it is rotating. The real effect would be to raise the elements one position and the one that is left above appears below and so on while it is on. I have no idea how it could be done. Thanks a lot
Image svg
icon_return_pump_1:
styles:
custom_fields:
icon:
- margin-left: -15%
custom_fields:
icon: >
[[[
let state = 'on';
return `
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="55.000000pt" height="55.000000pt" viewBox="0 0 512.000000 512.000000" preserveAspectRatio="xMidYMid meet">
<style>
.start {
}
.on {
}
.end {
}
.start_timeout {
}
.end_timeout {
}
#keyframes on {
0% {
transform: scale(0.5);
}
20% {
transform: scale(1);
}
40% {
transform: scale(1);
}
60% {
transform: scale(1);
}
80% {
transform: scale(1);
}
100% {
transform: scale(1);
}
}
.on {
animation: on 0.8s;
transform-origin: center;
}
</style>
<g transform="translate(0.000000,512.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
<path fill="#9da0a2" d="M895 4466 c-37 -17 -70 -52 -84 -89 -6 -16 -11 -78 -11 -138 0 -105 2 -113 39 -202 43 -100 85 -235 96 -310 l7 -47 498 0 498 0 7 47 c11 75 53 210 96 312 l41 94 -4 123 c-3 133 -10 153 -72 198 -27 21 -40 21 -554 23 -424 2 -533 0 -557 -11z"/>
<path fill="#9da0a2" d="M960 2400 l0 -1120 160 0 160 0 0 -80 0 -79 -429 -3 c-416 -3 -430 -4 -457 -24 -64 -47 -69 -63 -69 -214 0 -151 5 -167 69 -214 l27 -21 2139 0 2139 0 27 21 c64 47 69 63 69 214 0 151 -5 167 -69 214 -27 20 -40 21 -537 24 l-509 3 0 159 0 160 160 0 160 0 0 80 0 80 -480 0 -480 0 0 -80 0 -80 160 0 160 0 0 -160 0 -160 -880 0 -880 0 0 80 0 80 160 0 160 0 0 1120 0 1120 -480 0 -480 0 0 -1120z"/>
<path fill="#9da0a2" d="M2238 3439 l-158 -79 0 -960 0 -960 160 -80 160 -80 0 1120 c0 616 -1 1120 -2 1119 -2 0 -74 -36 -160 -80z"/>
<path fill="#9da0a2" d="M2560 2400 l0 -1120 160 80 160 80 0 960 0 960 -160 80 -160 80 0 -1120z"/>
<path fill="#9da0a2" d="M415 3346 c-41 -18 -83 -69 -90 -109 -3 -18 -5 -406 -3 -864 3 -822 3 -832 24 -859 39 -53 71 -69 134 -69 63 0 95 16 134 69 18 23 22 45 24 137 l4 109 79 0 79 0 0 640 0 640 -79 0 -79 0 -4 109 c-3 118 -12 141 -70 184 -33 25 -113 32 -153 13z"/>
<path fill="#9da0a2" d="M3040 3280 l0 -80 480 0 480 0 0 80 0 80 -480 0 -480 0 0 -80z"/>
<path fill="#9da0a2" d="M4160 2400 l0 -960 33 0 c65 1 174 31 257 71 164 81 297 253 335 434 11 54 15 155 15 455 0 439 -4 474 -71 610 -58 118 -161 221 -279 279 -83 40 -192 70 -257 71 l-33 0 0 -960z"/>
<path class="${state}" fill="#9da0a2" d="M3040 2880 l0 -160 480 0 480 0 0 160 0 160 -480 0 -480 0 0 -160z"/>
<path class="${state}" fill="#9da0a2" d="M3040 2400 l0 -160 480 0 480 0 0 160 0 160 -480 0 -480 0 0 -160z"/>
<path class="${state}" fill="#9da0a2" d="M3040 1920 l0 -160 480 0 480 0 0 160 0 160 -480 0 -480 0 0 -160z"/>
</g>
</svg>
`;
]]]
This is how I would do it: I would replace those paths with a very thick line with a stroke-dasharay and I would animate the stroke-dashoffset of the line.
#keyframes on {
100% {
stroke-dashoffset:1920
}
}
.on {
animation: on 0.8s infinite;
}
<svg viewBox="0 0 512.000000 512.000000"width="200" >
<g transform="translate(0.000000,512.000000) scale(0.100000,-0.100000)" fill="#9da0a2" stroke="none">
<path d="M895 4466 c-37 -17 -70 -52 -84 -89 -6 -16 -11 -78 -11 -138 0 -105 2 -113 39 -202 43 -100 85 -235 96 -310 l7 -47 498 0 498 0 7 47 c11 75 53 210 96 312 l41 94 -4 123 c-3 133 -10 153 -72 198 -27 21 -40 21 -554 23 -424 2 -533 0 -557 -11z" />
<path d="M960,2400L960,1280L1120,1280L1280,1280L1280,1200L1280,1121L851,1118C435,1115,421,1114,394,1094C330,1047,325,1031,325,880C325,729,330,713,394,666L421,645L2560,645L4699,645L4726,666C4790,713,4795,729,4795,880C4795,1031,4790,1047,4726,1094C4699,1114,4686,1115,4189,1118L3680,1121L3680,1280L3680,1440L3040,1440L3200,1440L3360,1440L3360,1280L3360,1120L2480,1120L1600,1120L1600,1200L1600,1280L1760,1280L1920,1280L1920,2400L1920,3520L1440,3520L960,3520L960,2400Z" />
<path d="M2238 3439 l-158 -79 0 -960 0 -960 160 -80 160 -80 0 1120 c0 616 -1 1120 -2 1119 -2 0 -74 -36 -160 -80z" />
<path d="M2560 2400 l0 -1120 160 80 160 80 0 960 0 960 -160 80 -160 80 0 -1120z" />
<path d="M4160 2400 l0 -960 33 0 c65 1 174 31 257 71 164 81 297 253 335 434 11 54 15 155 15 455 0 439 -4 474 -71 610 -58 118 -161 221 -279 279 -83 40 -192 70 -257 71 l-33 0 0 -960z" />
<path d="M415 3346 c-41 -18 -83 -69 -90 -109 -3 -18 -5 -406 -3 -864 3 -822 3 -832 24 -859 39 -53 71 -69 134 -69 63 0 95 16 134 69 18 23 22 45 24 137 l4 109 79 0 79 0 0 640 0 640 -79 0 -79 0 -4 109 c-3 118 -12 141 -70 184 -33 25 -113 32 -153 13z"/>
<path class="on" stroke="#9da0a2" stroke-width="970" d="M3530 3360V1440" stroke-dasharray="250 150" />
</g>
</svg>
Observation: I've modified the second path since I wanted to remove the bit corresponding to the rotating engine.

(Next.js) How can I change the color of SVG in next/image?

import Image from 'next/image'
...
<Image src="/emotion.svg" alt="emtion" width={50} height={50} />
I want to change the SVG color in next/image.
But in CSS,
img {
fill="#ffffff"
}
is not working.
How can I solve this?
I recommend you to not to use svg files directly, but use Playground SVG (https://react-svgr.com/playground/), convert the file to JS and then you can change the color and other stuff by props.
I am using Next/Image and inserting the SVG url dynamically. For this case I needed to use css filter to somehow change the SVG color.
https://developer.mozilla.org/en-US/docs/Web/CSS/filter
I think the best solution here is not to use Image. by default the nextjs Image component is not doing any optimization on svg's. You will then have multiple options/solutions to your problem:
Extend webpack config on your next.config.js file using #svgr/webpack:
// next.config.js
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
use: ["#svgr/webpack"]
});
return config;
}
// The file where u want to import the svg
import YourSvg from './public/yourSvg.svg'
// I recommend using the `currentColor` property on your svg, but you can also pass the color as prop
<YourSvg color="red" />
Using babel-plugin-inline-react-svg: https://github.com/vercel/next.js/discussions/20993#discussioncomment-760119
Well, the solution that I have found is practical. I edit the svg and change the color. When I select the svg in vscode it shows me all its configuration.
This svg will change the color to pink:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="128.000000pt" height="128.000000pt" viewBox="0 0 128.000000 128.000000"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,128.000000) scale(0.100000,-0.100000)"
fill="#D2485A" stroke="none">
<path d="M647 1233 c-13 -15 -17 -38 -17 -102 l0 -83 -84 -97 c-47 -53 -109
-123 -140 -156 l-55 -60 -1 58 0 57 -175 0 -175 0 0 -400 0 -400 175 0 175 0
0 31 c0 23 4 30 14 26 8 -3 57 -20 108 -38 l93 -33 265 -3 c300 -5 316 -2 354
72 17 33 18 43 8 81 -10 39 -9 48 8 76 22 36 26 98 9 129 -8 16 -5 26 15 50
28 33 35 93 16 129 -8 15 -4 27 15 56 30 44 32 88 5 131 -34 56 -61 63 -235
63 -106 0 -155 3 -155 11 0 6 7 36 16 66 25 85 17 198 -18 251 -52 78 -182
128 -221 85z m113 -85 c42 -29 70 -82 70 -133 0 -22 -13 -87 -30 -145 -16 -58
-30 -108 -30 -112 0 -5 87 -8 193 -8 158 0 197 -3 215 -16 40 -28 26 -80 -24
-90 -30 -6 -34 -10 -34 -39 0 -24 6 -34 24 -43 50 -22 41 -85 -14 -97 -25 -6
-30 -11 -30 -37 0 -20 8 -37 25 -50 27 -21 33 -59 12 -76 -6 -6 -24 -13 -40
-17 -23 -6 -27 -13 -27 -40 0 -25 5 -35 20 -40 24 -7 37 -51 23 -74 -9 -14
-44 -16 -279 -16 l-269 0 -107 39 -108 38 0 215 0 216 88 95 c262 286 262 285
262 379 0 82 6 87 60 51z m-480 -698 l0 -320 -100 0 -100 0 0 320 0 320 100 0
100 0 0 -320z"/>
</g>
</svg>
here where it says fill I change the color:
<g transform="translate(0.000000,128.000000) scale(0.100000,-0.100000)"
fill="#D2485A" stroke="none">

SVG IMAGE clip-path does not want move 40% to the right

How to move SVG clip-path 40% to the right ?
https://jsfiddle.net/vtgmsyg0/
My nested "svg" is not responding to specified for it "x coordinate" of value 40%...
I used nested SVG for this...
<svg width="120%" height="855">
<svg width="900px" height="855px" x="40%" y="10">
<clipPath id="uniqueId" >
<path transform="translate(0,664.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none"d="M23 6616 c32 -51 1686 -2932 1691 -2946 4 -11 -57 -52 -226 -150
-127 -75 -231 -139 -232 -142 -2 -11 1959 -3368 1967 -3368 5 0 505 288 1112
640 607 352 1107 640 1112 640 4 0 37 -50 72 -110 l64 -110 47 27 c26 16 626
368 1334 783 922 541 1285 759 1283 770 -3 8 -320 562 -706 1230 -387 668
-703 1219 -704 1223 -2 8 550 327 2073 1198 l575 329 -4100 -1 c-2255 0 -4387
2 -4738 4 l-638 5 14 -22z"/>
</clipPath>
</svg>
</svg>
Change the X value in the translate() component of the transform attribute. To move it left, make it a negative value. 40% of 900 is 360 so the following should be about right:
transform="translate(-360, 664.000000) scale(0.100000,-0.100000)"
body {
overflow-x:hidden;
}
.mega {
position:absolute;
width:120%;
height:100%;
clip-path: url("#uniqueId");
overflow-x:hidden;
}
<!-- This is the video used for SVG clipping -->
<video autoplay="" loop="" class="mega">
<source src="https://gekos.pl/wp-content/themes/gekos/video/slider.mp4" type="video/mp4">
<source src="https://gekos.pl/wp-content/themes/gekos/video/slider.ogg" type="video/ogg">
<source src="https://gekos.pl/wp-content/themes/gekos/video/slider.webm" type="video/webm">
</video>
<!-- Code below is where I am applying clipping the the video, my nested "svg" is not responding to specified for
it "x coordinate" of value 40% -->
<svg width="120%" height="855">
<svg width="900px" height="855px">
<clipPath id="uniqueId" >
<path transform="translate(-360, 664.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none"d="M23 6616 c32 -51 1686 -2932 1691 -2946 4 -11 -57 -52 -226 -150
-127 -75 -231 -139 -232 -142 -2 -11 1959 -3368 1967 -3368 5 0 505 288 1112
640 607 352 1107 640 1112 640 4 0 37 -50 72 -110 l64 -110 47 27 c26 16 626
368 1334 783 922 541 1285 759 1283 770 -3 8 -320 562 -706 1230 -387 668
-703 1219 -704 1223 -2 8 550 327 2073 1198 l575 329 -4100 -1 c-2255 0 -4387
2 -4738 4 l-638 5 14 -22z"/>
</clipPath>
</svg>
</svg>

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/

r how to subtract elements of different length matrices

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().

Resources