Home > database >  How to scale this dataset with pre-defined user R function
How to scale this dataset with pre-defined user R function

Time:10-06

Assume we have the following dataset :

DB_defects_splited_pcb_type=structure(list(pcb_type = c("01", "01", "01", "01", "01", "01", 
    "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
    "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
    "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
    "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
    "01", "01", "01", "01", "01", "01", "01", "01", "01", "01"), 
        Defect_type = c("Open_circuit", "Mouse_bite", "Spur", "Mouse_bite", 
        "Open_circuit", "Spurious_copper", "Spurious_copper", "Short", 
        "Mouse_bite", "Spur", "Short", "Open_circuit", "Spur", "Short", 
        "Mouse_bite", "Spur", "Missinghole", "Missinghole", "Spur", 
        "Short", "Short", "Spur", "Spur", "Spur", "Spur", "Spur", 
        "Spur", "Spur", "Spur", "Short", "Missinghole", "Missinghole", 
        "Spurious_copper", "Spur", "Mouse_bite", "Open_circuit", 
        "Open_circuit", "Spurious_copper", "Open_circuit", "Missinghole", 
        "Open_circuit", "Mouse_bite", "Open_circuit", "Spurious_copper", 
        "Spurious_copper", "Open_circuit", "Open_circuit", "Spurious_copper", 
        "Spur", "Short", "Missinghole", "Mouse_bite", "Open_circuit", 
        "Mouse_bite", "Spurious_copper", "Missinghole", "Short", 
        "Short", "Spurious_copper", "Spurious_copper"), sd1 = c(0.16810838404786, 
        0.16809449227504, 0.168092894645025, 0.168094832629513, 0.168108496900373, 
        0.16808796605408, 0.168088591853615, 0.168102085445051, 0.168086823444584, 
        0.168093228577678, 0.168102244708701, 0.16810961025903, 0.168093288099756, 
        0.168102015142555, 0.168094189412324, 0.168094173053058, 
        0.167684720858417, 0.16779485382475, 0.168093450809282, 0.168105849190783, 
        0.168104926560611, 0.168091792928968, 0.168092047028413, 
        0.168093088373005, 0.168093038482056, 0.168092960361196, 
        0.168093164238512, 0.168093383189173, 0.168093871326839, 
        0.168106777196285, 0.167850574237067, 0.167836974029529, 
        0.168091109609073, 0.168093400227581, 0.168094529884508, 
        0.168108489040613, 0.168106872394283, 0.168088610795341, 
        0.168108675194047, 0.167861036998376, 0.168108544291683, 
        0.168094933022067, 0.168108387006541, 0.168091040500625, 
        0.168088539848386, 0.168108969239718, 0.168108757028795, 
        0.168088949614829, 0.168093365072221, 0.168104808928633, 
        0.167849511559066, 0.168094528654114, 0.168109143340974, 
        0.168090824029403, 0.168088117047113, 0.167846993222298, 
        0.168101530362186, 0.168103549168476, 0.168087620146795, 
        0.168088354977095), sum1 = c(393024.49019608, 393006.435294119, 
        393038.321568629, 393000.043137256, 393025.243137256, 393096.188235296, 
        393088.513725492, 393133.643137256, 392970.109803923, 393029.701960786, 
        393133.360784315, 393002.654901962, 393072.11764706, 393139.705882354, 
        393016.537254903, 393079.396078433, 391730.392156864, 391962.23137255, 
        393022.015686276, 393058.721568629, 393089.870588237, 393060.266666668, 
        393038.305882354, 393032.058823531, 393057.133333335, 393039.964705884, 
        393058.035294119, 393025.564705884, 393070.749019609, 393040.368627452, 
        392207.17647059, 392231.066666668, 393057.639215688, 393022.666666668, 
        393009.505882354, 393021.513725492, 393006.49019608, 393089.494117649, 
        393021.564705884, 392129.223529413, 393021.666666668, 392999.811764707, 
        393022.400000001, 393057.552941178, 393088.800000002, 393013.274509805, 
        393016.419607845, 393084.886274511, 393062.533333335, 393072.525490198, 
        392252.168627452, 393010.905882354, 393010.266666668, 392982.827450982, 
        393094.101960786, 392131.635294119, 393139.125490198, 393101.400000001, 
        393100.878431374, 393090.341176472), sd2 = c(0.102938617042646, 
        0.102922663396653, 0.102919310077427, 0.102928469086841, 
        0.102939203427981, 0.102884537845135, 0.102906858028489, 
        0.102913161143599, 0.102923759532971, 0.102920024074789, 
        0.10291421126303, 0.102938086713294, 0.102915095129317, 0.10291517600863, 
        0.102925489451006, 0.102915520714993, 0.102794837852993, 
        0.102852767093549, 0.102918426502874, 0.102922469496974, 
        0.102924112959045, 0.102917283917233, 0.102917724015655, 
        0.102919556699571, 0.102917590776993, 0.102920511522864, 
        0.102918408473999, 0.102920633644626, 0.102917212166919, 
        0.102921104870415, 0.102853690122406, 0.102847598133362, 
        0.102903881555672, 0.102919480412981, 0.102925825883113, 
        0.102927629117558, 0.102939412767276, 0.102896158734185, 
        0.102928635740128, 0.102871174707173, 0.102929523746327, 
        0.102931016844216, 0.102934645123377, 0.102901626389135, 
        0.102898784071186, 0.102929619425816, 0.10293562877321, 0.102897214435311, 
        0.102915755777881, 0.102917476251721, 0.102855965707123, 
        0.102931325403981, 0.102933744247047, 0.10292859583443, 0.102902016514033, 
        0.102873663307758, 0.102908683817266, 0.102918446477889, 
        0.102909082126758, 0.10290897864411), sum2 = c(1577184.62352943, 
        1577230.48235296, 1577278.81960787, 1577218.07058826, 1577196.73725493, 
        1577664.79607845, 1577518.07058826, 1577495.32941179, 1577196.3529412, 
        1577311.50196081, 1577493.90588238, 1577169.72156865, 1577312.17647061, 
        1577487.55686277, 1577244.65882355, 1577319.65490198, 1576656.00392159, 
        1576755.027451, 1577291.98823532, 1577351.15686277, 1577362.627451, 
        1577300.9529412, 1577278.1529412, 1577288.17254904, 1577295.79215689, 
        1577299.18039218, 1577298.11372551, 1577275.54901963, 1577311.23921571, 
        1577367.66666669, 1576893.9529412, 1576905.27450983, 1577444.74117649, 
        1577292.35686277, 1577222.10196081, 1577236.6784314, 1577192.53333336, 
        1577563.83137257, 1577245.1529412, 1576836.25882355, 1577243.0784314, 
        1577182.4156863, 1577214.32156865, 1577487.87058826, 1577568.32941179, 
        1577217.5529412, 1577199.87058826, 1577559.01176473, 1577302.64313728, 
        1577376.8901961, 1576925.41176473, 1577179.10196081, 1577194.36862747, 
        1577203.71764708, 1577562.43137257, 1576861.36470591, 1577517.54117649, 
        1577432.29411767, 1577535.25490198, 1577507.45098042), sd3 = c(0.141956498680034, 
        0.141940871317941, 0.14193767691202, 0.14194194746325, 0.141957057393608, 
        0.141922107752678, 0.141930241787167, 0.141946798647658, 
        0.141936518513237, 0.141938402646598, 0.141946953429555, 
        0.141956437315321, 0.141937831648352, 0.141946929492314, 
        0.14194016916176, 0.141939218885203, 0.141595946762206, 0.141728118968029, 
        0.141937954510598, 0.141950152244626, 0.141950268578409, 
        0.141935776889511, 0.141937699685645, 0.141937970123023, 
        0.141935951195291, 0.141938139845344, 0.141938160642707, 
        0.14193848184079, 0.141938606841573, 0.141950305410455, 0.14176935089913, 
        0.141729693604049, 0.141934216456196, 0.141937714743736, 
        0.141940978773544, 0.141959830942916, 0.14195378655123, 0.141930616929521, 
        0.141957245419727, 0.141792404053674, 0.141957666593585, 
        0.141942330760477, 0.141955517656066, 0.141934847133214, 
        0.141927246794768, 0.14195842640552, 0.141957624934562, 0.141932504485756, 
        0.141937788785944, 0.141948840807972, 0.141749832435663, 
        0.141942138925069, 0.141957292362582, 0.141937515209358, 
        0.141926248293953, 0.14175494700043, 0.141945515907224, 0.141948215042682, 
        0.141928127786719, 0.141927259090039), sum3 = c(516655.066666672, 
        516641.41568628, 516681.572549025, 516632.894117652, 516649.282352946, 
        516843.317647064, 516773.874509809, 516795.145098044, 516621.623529417, 
        516684.537254907, 516791.152941181, 516642.184313731, 516714.854901966, 
        516793.117647064, 516656.75686275, 516722.501960789, 515500.145098044, 
        515761.901960789, 516680.01568628, 516726.901960789, 516743.874509809, 
        516703.552941181, 516681.027450985, 516677.27058824, 516711.203921574, 
        516678.933333338, 516700.827450985, 516667.623529417, 516713.678431378, 
        516719.733333338, 515977.650980397, 515957.576470593, 516724.560784319, 
        516682.827450985, 516644.627450985, 516625.231372554, 516657.44313726, 
        516768.87058824, 516645.160784319, 515927.278431378, 516647.090196083, 
        516628.792156868, 516663.545098044, 516722.878431378, 516798.031372554, 
        516637.054901966, 516643.745098044, 516752.309803927, 516704.882352946, 
        516774.309803927, 515992.305882358, 516634.580392162, 516632.133333338, 
        516645.266666672, 516809.478431378, 515900.031372554, 516819.12941177, 
        516765.231372554, 516793.721568632, 516793.011764711), ` Min. 1` = c(0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0), `1st Qu. 1` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `Median 1` = c(0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196, 0.00784313725490196, 
        0.00784313725490196, 0.00784313725490196), `Mean 1` = c(0.0816772023407018, 
        0.0816734502236772, 0.081680076736172, 0.0816721218242964, 
        0.0816773588147392, 0.0816921024179296, 0.0816905075237039, 
        0.0816998861863269, 0.0816659011663363, 0.0816782854344302, 
        0.0816998275085627, 0.0816726645936138, 0.0816871001385431, 
        0.0817011461283164, 0.0816755495836805, 0.0816886127209056, 
        0.0814082666635765, 0.0814564468126574, 0.0816766880952972, 
        0.0816843162046256, 0.0816907895029587, 0.0816846373023902, 
        0.0816800734762963, 0.081678775230766, 0.0816839861422029, 
        0.0816804182081601, 0.0816841735850605, 0.0816774256421929, 
        0.0816868157143812, 0.0816805021499615, 0.0815073505879538, 
        0.0815123153787688, 0.0816840912731969, 0.0816768233801419, 
        0.0816740883443614, 0.0816765837792721, 0.0816734616332424, 
        0.0816907112659399, 0.0816765943738682, 0.0814911506352576, 
        0.0816766155630608, 0.0816720737411286, 0.081676767962254, 
        0.0816840733438802, 0.0816905670164365, 0.0816748715295178, 
        0.0816755251346123, 0.0816897536774292, 0.0816851083544406, 
        0.0816871848953134, 0.0815167007266637, 0.081674379288275, 
        0.0816742464483369, 0.0816685441106261, 0.081691668854451, 
        0.0814916518411593, 0.0817010255129125, 0.0816931855116581, 
        0.0816930771207883, 0.0816908872992321), `3rd Qu. 1` = c(0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608, 0.0431372549019608, 
        0.0431372549019608, 0.0431372549019608), `Max. 1` = c(1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1), ` Min. 2` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `1st Qu. 2` = c(0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118, 0.270588235294118, 
        0.270588235294118, 0.270588235294118), `Median 2` = c(0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235, 0.341176470588235, 
        0.341176470588235, 0.341176470588235), `Mean 2` = c(0.327765904766869, 
        0.327775435013716, 0.327785480320937, 0.327772855637004, 
        0.327768422205941, 0.327865692824415, 0.327835200761324, 
        0.32783047475641, 0.327768342338985, 0.327792272272127, 0.327830178922683, 
        0.327762807884877, 0.327792412446786, 0.327828859487961, 
        0.327778381126454, 0.327793966592565, 0.327656048582972, 
        0.327676627363811, 0.327788216986655, 0.327800513238103, 
        0.327802897022268, 0.327790080005664, 0.327785341776216, 
        0.327787424021871, 0.327789007506532, 0.327789711639701, 
        0.327789489968148, 0.327784800636836, 0.327792217669208, 
        0.327803944257363, 0.32770549845367, 0.327707851269015, 0.327819961657015, 
        0.327788293593736, 0.32777369342508, 0.32777672266465, 0.327767548559232, 
        0.327844710633948, 0.327778483812541, 0.327693508630546, 
        0.327778052693969, 0.327765445939353, 0.327772076526692, 
        0.327828924685476, 0.327845645403328, 0.327772748061103, 
        0.327769073366129, 0.327843709037114, 0.32779043125728, 0.327805861064322, 
        0.327712036134549, 0.327764757290594, 0.327767929964698, 
        0.327769872850664, 0.327844419690034, 0.327698726061734, 
        0.327835090740517, 0.327817374945583, 0.327838771955243, 
        0.32783299382542), `3rd Qu. 2` = c(0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549, 0.36078431372549, 0.36078431372549, 
        0.36078431372549, 0.36078431372549), `Max. 2` = c(1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
        ` Min. 3` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0), `1st Qu. 3` = c(0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098, 0.0392156862745098, 
        0.0392156862745098, 0.0392156862745098), `Median 3` = c(0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647, 0.0588235294117647, 
        0.0588235294117647, 0.0588235294117647), `Mean 3` = c(0.107369747873547, 
        0.107366910966647, 0.107375256248648, 0.107365140039129, 
        0.107368545794352, 0.1074088696428, 0.107394438172715, 0.107398858564275, 
        0.107362797818381, 0.107375872365171, 0.107398028925888, 
        0.107367070700561, 0.107382172890088, 0.10739843722533, 0.107370099125162, 
        0.107383762079531, 0.107129735444292, 0.107184132991457, 
        0.107374932705976, 0.107384676474688, 0.107388203660283, 
        0.107379824149587, 0.107375142967965, 0.107374362227715, 
        0.107381414153999, 0.107374707774548, 0.10737925774617, 0.10737235740411, 
        0.107381928399404, 0.107383186711456, 0.107228969322956, 
        0.107224797496924, 0.107384189938227, 0.10737551703871, 0.107367578426214, 
        0.10736354758981, 0.107370241744727, 0.107393398272341, 0.10736768926199, 
        0.107218501046852, 0.107368090226711, 0.107364287581612, 
        0.107371509836406, 0.107383840316549, 0.107399458381419, 
        0.107366004721181, 0.107367395058201, 0.107389956658485, 
        0.107380100424059, 0.107394528634268, 0.107232014861904, 
        0.107365490475776, 0.107364981935154, 0.107367711266152, 
        0.10740183727577, 0.10721283864262, 0.107403842914344, 0.107392641981159, 
        0.107398562730548, 0.107398415221168), `3rd Qu. 3` = c(0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725, 0.0901960784313725, 
        0.0901960784313725, 0.0901960784313725), `Max. 3` = c(1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1)), row.names = c(231L, 116L, 481L, 129L, 249L, 587L, 
    596L, 360L, 134L, 468L, 359L, 248L, 472L, 357L, 133L, 477L, 9L, 
    13L, 463L, 347L, 350L, 482L, 474L, 465L, 476L, 467L, 471L, 466L, 
    473L, 349L, 10L, 20L, 593L, 464L, 120L, 234L, 243L, 590L, 239L, 
    1L, 236L, 121L, 245L, 592L, 591L, 238L, 232L, 588L, 479L, 364L, 
    14L, 118L, 250L, 124L, 594L, 3L, 362L, 361L, 585L, 584L), class = "data.frame")

I tried to scale using this pre-defined function :

scaling<-function(x){
  
  if(var(x) == 0){ return(1)}
  
  if(var(x)!=0) {return(scale(x))}
  
  if(is.numeric(x)==FALSE) { return(x)}
  
  
}

This gives the following output :

head(as.data.frame(apply(DB_defects_splited_pcb_type[[1]], 2, scaling)),5) 

   pcb_type Defect_type       sd1     sum1       sd2    sum2       sd3     sum3 Min. 1 1st Qu. 1    Median 1
1        01 Missinghole 0.1678610 392129.2 0.1028712 1576836 0.1417924 515927.3      0         0 0.007843137
2        01 Missinghole 0.1678171 392008.7 0.1028679 1576775 0.1417563 515818.5      0         0 0.007843137
3        01 Missinghole 0.1678470 392131.6 0.1028737 1576861 0.1417549 515900.0      0         0 0.007843137
4        01 Missinghole 0.1678477 392215.7 0.1028524 1576919 0.1417567 515963.7      0         0 0.007843137
5        01 Missinghole 0.1677222 391646.1 0.1028411 1576670 0.1416830 515495.5      0         0 0.007843137

For example it seems that column sum1 is not scaled. This is the question to solve.

Thanks

CodePudding user response:

We may use across within mutate to apply the scaling function on the numeric columns (where(is.numeric))

library(dplyr)
out <- DB_defects_splited_pcb_type %>%
   mutate(across(where(is.numeric), scaling))

-output

> head(out)
    pcb_type     Defect_type       sd1      sum1        sd2        sum2        sd3      sum3  Min. 1 1st Qu. 1 Median 1    Mean 1 3rd Qu. 1 Max. 1
231       01    Open_circuit 0.4697013 0.2712107  1.0328080 -0.44188693 0.53953184 0.1736352       1         1        1 0.2712107         1      1
116       01      Mouse_bite 0.3202207 0.2144640  0.4295916 -0.22003298 0.32194592 0.1257385       1         1        1 0.2144640         1      1
481       01            Spur 0.3030296 0.3146828  0.3028007  0.01381101 0.27746895 0.2666353       1         1        1 0.3146828         1      1
129       01      Mouse_bite 0.3238831 0.1943734  0.6491080 -0.28007811 0.33692952 0.0958392       1         1        1 0.1943734         1      1
249       01    Open_circuit 0.4709157 0.2735772  1.0549796 -0.38328364 0.54731102 0.1533400       1         1        1 0.2735772         1      1
587       01 Spurious_copper 0.2499962 0.4965583 -1.0119572  1.88107220 0.06069342 0.8341441       1         1        1 0.4965583         1      1
     Min. 2 1st Qu. 2 Median 2      Mean 2 3rd Qu. 2 Max. 2  Min. 3 1st Qu. 3 Median 3    Mean 3 3rd Qu. 3 Max. 3
231       1         1        1 -0.44188693         1      1       1         1        1 0.1736352         1      1
116       1         1        1 -0.22003298         1      1       1         1        1 0.1257385         1      1
481       1         1        1  0.01381101         1      1       1         1        1 0.2666353         1      1
129       1         1        1 -0.28007811         1      1       1         1        1 0.0958392         1      1
249       1         1        1 -0.38328364         1      1       1         1        1 0.1533400         1      1
587       1         1        1  1.88107220         1      1       1         1        1 0.8341441         1      1

The column first is selected with [[ in the OP's code

 DB_defects_splited_pcb_type[[1]]

CodePudding user response:

You might define the logic of the scaling function a little nicer if possible.

scaling <- function(x) {
  if (!is.numeric(x)) x
  else if (var(x) != 0) as.vector(scale(x))
  else rep.int(1, length(x))
}

And then actually just lapply:

DB_defects_splited_pcb_type[] <- lapply(DB_defects_splited_pcb_type, scaling)

head(DB_defects_splited_pcb_type)
#     pcb_type     Defect_type       sd1      sum1        sd2        sum2        sd3      sum3  Min. 1
# 231       01    Open_circuit 0.4697013 0.2712107  1.0328080 -0.44188693 0.53953184 0.1736352       1
# 116       01      Mouse_bite 0.3202207 0.2144640  0.4295916 -0.22003298 0.32194592 0.1257385       1
# 481       01            Spur 0.3030296 0.3146828  0.3028007  0.01381101 0.27746895 0.2666353       1
# 129       01      Mouse_bite 0.3238831 0.1943734  0.6491080 -0.28007811 0.33692952 0.0958392       1
# 249       01    Open_circuit 0.4709157 0.2735772  1.0549796 -0.38328364 0.54731102 0.1533400       1
# 587       01 Spurious_copper 0.2499962 0.4965583 -1.0119572  1.88107220 0.06069342 0.8341441       1
#     1st Qu. 1 Median 1    Mean 1 3rd Qu. 1 Max. 1  Min. 2 1st Qu. 2 Median 2      Mean 2 3rd Qu. 2 Max. 2
# 231         1        1 0.2712107         1      1       1         1        1 -0.44188693         1      1
# 116         1        1 0.2144640         1      1       1         1        1 -0.22003298         1      1
# 481         1        1 0.3146828         1      1       1         1        1  0.01381101         1      1
# 129         1        1 0.1943734         1      1       1         1        1 -0.28007811         1      1
# 249         1        1 0.2735772         1      1       1         1        1 -0.38328364         1      1
# 587         1        1 0.4965583         1      1       1         1        1  1.88107220         1      1
#      Min. 3 1st Qu. 3 Median 3    Mean 3 3rd Qu. 3 Max. 3
# 231       1         1        1 0.1736352         1      1
# 116       1         1        1 0.1257385         1      1
# 481       1         1        1 0.2666353         1      1
# 129       1         1        1 0.0958392         1      1
# 249       1         1        1 0.1533400         1      1
# 587       1         1        1 0.8341441         1      1

The "pcb_type" column doesn't get scaled because it's character, check that using str(DB_defects_splited_pcb_type).

  •  Tags:  
  • r
  • Related