Errors in calculating row ranges

Hi everyone,

I´m stuggling to calculate the row ranges for the data set below. I tried rowRanges of matrixstats as well as snpStats of BiocManager, both ending up in errors. What am I doing wrong?

Thank you so much for any advice!!

data.frame(
check.names = FALSE,
2017 = c(-29.6765588373183,NA,-89.2790876524891,
-99.5039287808624,-85.8044413319264,-81.6880304924956,
-61.6649799233737,-95.9201077199282,-76.9212542696249,
-52.8207862180081,-90.690884048419,-98.9861843471265,
-95.3035792168285,-94.4460797686721,-66.8589951783467,-96.4280665380505,
-94.3530923544558,-90.5172796372587,NA,-52.1448699910989,
-99.749258105917,-76.7606641500238,-92.3517510751362,
-61.3614550377808,-89.9173970914736,-72.8208312371765,
-93.6928351228182,-80.1803724252566,-65.7165151211417,-16.9720678050042,
-39.5262772714598,-92.6465931062071,-83.9073615089967,
-93.3626316929084,-94.0751798978874,-92.0348869767974,
-99.6849495373757,-99.4372034136017,-84.0900171616826,
-96.6212240801109,-95.9476815180627,-93.2603935722358,-85.4464382341284,
-63.7648377287265,-82.4181974607104,-96.5646946921266,
-78.2312108404039,-78.3123358585397,-99.7308258872328,
-73.7570600876515,-86.1038047858592,-98.3034928032794,
1.74448959876793,-89.1012227056468,-99.065009336879,-73.851678983977,
-64.901612001988,-73.1464505313982,-74.1900032520661,
-93.7320144264087,-69.23374856424,-82.3809991785621,-86.8560017899225,
-17.7785208166606,-91.0114080803448,-41.4733192727366,
-35.9850062592466,-81.424690391524,-51.0787904816785,
-72.1417116958318,-78.7342808459866,-97.3218392282223,
-65.9796655310339,-59.9721940050773,-78.9439624919285,-68.9168523027509,
-94.1731338622074,-97.128980912445,-74.1813669650962,
-91.3531365349389,-56.8677168711329,-96.8072089279812,
-47.4896618270163,-86.7712684145171,29.7822249909152,-93.1059293516303,
-88.3806470768993,-85.3967654377719,-39.4987300221875,NA,
NA,-95.0238756040226,NA,-23.1349988555655,
-92.6335162990967,-95.3525458304003,-86.6009367444409,-92.1557650612428,
-80.2107698845063,-94.9213888223018,-66.8070099901559,
-54.8243899233657,-65.9627593155198,-82.8539275496009,
-55.2432529203777,-46.4123147889692,-38.6842092851045,
-79.0715711840073),
2018 = c(-64.1666519351693,-60.4951830138689,
-88.3812908403072,-99.4453492319537,-85.1840785602037,
-74.8927001146028,-67.50034588967,193.50555145579,-69.6587622123482,
-62.9400750278181,-90.5364373853199,-98.8384040512051,
-95.337321076871,-88.6414474547727,-66.8471045702422,
-96.1168493115813,-94.478647451438,-91.3361487440683,
-83.5182199275366,-50.538914624392,-99.6923761336304,-91.6703039694063,
-92.2044038354935,-61.2230732482101,-89.5624787221672,
-62.7916749655823,-93.7437924449398,-77.7457397785842,
-72.7154217453207,-10.0972330944652,-45.0347580329232,-89.0996321772525,
0.148221343873518,-92.1356393994418,-88.9770992164465,
-90.7051649078348,-99.0526090940533,-99.5670203556634,
-78.114157316486,-96.415672885436,-97.0843635317534,
-92.9576950997009,-85.8829423810822,-65.0391632450655,-85.5110184242504,
-96.6077361410479,-79.0663271387746,-73.0318193509847,
-99.7387303113601,-76.6707276926546,-80.1428210594541,
-98.4769266307728,-77.6872056035559,-87.2339755537513,
-99.3978937863108,-76.7120298327293,-23.3502520385367,-72.071359073998,
-74.3927484215659,-93.5928282947483,-71.6389610520571,
-82.1841009948805,-84.9844247391383,-53.7254523698955,
-88.2662407194213,-63.0190522050583,-43.929786105745,-81.7845072125137,
-57.9502413878729,-68.1688384219832,-82.7001721679631,
-97.566536642282,-64.5765269562894,-65.5546108159183,
-78.410140968326,-67.1435612117495,-94.7737150438765,-97.8621897853496,
-75.3091059211845,-92.8491175078267,-53.3434705920654,
-87.2095980583138,-49.1673111636239,-86.9439840959258,
6.9406777485948,-91.8009445686755,-95.2311631521541,
-85.8052146658692,-42.8530980402751,-86.481261154075,NA,-95.3908210354497,
-97.8066569237672,-33.7531726019684,-90.7812104473024,
-95.9013015913439,-96.0369282954475,-92.0167340456559,
-81.2336241896757,-94.4871431112627,-69.8517219428956,
-64.4368669756567,-74.9723801944005,-85.7580493399783,-62.3129144022934,
-47.3420356236046,-43.0757758339843,-91.3752001678967),
2019 = c(-52.5696389710396,-68.5859389725439,
-98.9198841135791,-91.1648878522288,-85.4167659140072,
-75.1679467628325,-74.3341913058782,-95.4214517281169,
-69.5173581710415,-59.371765611653,-90.2105027064727,-97.996423458458,
-93.6959663716509,-87.3466553517521,-64.7361516754657,
-96.2646796178946,-94.6184425052621,-92.4227306411747,
-82.9866576317588,-64.8114393147269,-99.6927334644878,
-89.9022039235116,-91.8921391121501,-66.4934373915223,-90.4423277871038,
-69.0347377400812,-93.7925991974787,-76.4611034129945,
-82.1309258348389,10.8947303203541,-46.2574242952489,
-88.992547499454,-23.7489578885297,-94.0545737884364,-89.6833568278157,
-90.5186827584533,-99.1492084977168,-99.6598333226596,
-71.6029434593788,-96.5069867716379,-97.4611072895819,
-94.5044969791159,-86.6755030016724,-71.0509586901004,
-85.2974675293488,-96.6049080091195,-81.8043386220207,-85.2786448914254,
-99.749057617114,-78.3286924577987,-84.0442180278997,
-98.8111824042997,-36.6090046595856,-84.4005832140837,
-99.3146914144516,-77.9232549556478,-19.6704384514426,-68.2154610327512,
-76.2966695806019,-92.6376401125291,-66.8606547900031,
-79.8833006622872,-88.2346731343223,-44.7293653749218,
-83.9972209991509,-59.2997889157415,-50.1050012994881,
-78.4425099438145,-61.4091634192799,-73.5430250932609,-85.0166082647094,
-96.9285765769308,-71.8220228113127,-67.3163833601097,
-64.2321302531428,-68.1670302711329,-94.5015491020633,
-97.4300272763409,-76.833321904416,-91.0245215550269,
-50.8743281788472,-99.8606664475523,-49.4950050860066,-87.8712042680708,
-19.204877449713,-91.3732514519792,-93.6719753445343,
-89.7686688311688,-58.5144618717387,-86.0970522803115,NA,
-95.8442077289386,-94.8703410105973,-60.0065589243364,
-90.5510403088411,-91.9867610166377,-94.0931738541007,-93.0914010685335,
-77.5449092017371,-95.1595498192229,-79.682683700729,
-58.3374743396484,-59.6462818607472,-79.7435118338641,
-66.6772189158508,42.2393656639219,-51.8601974622158,
-89.3567480925455),
2020 = c(-60.497486599285,-74.2630607258552,
-99.048975073046,-94.8555487419248,-87.35265797426,
-78.6036965320539,-68.3986489048762,-44.0030164110681,-75.2033353611765,
-63.707434822513,-91.179475911318,-97.8606571078287,
-96.2395936327372,-87.1911279391413,-61.587412113665,
-96.1577754075495,-94.4178148872262,-92.1399352140724,-83.888876357997,
-40.0249325279527,-99.6985943445821,-87.6968598982306,
-92.4446010168752,-60.1327581807744,-91.2465161064426,
-73.6210013740106,-93.4498404233795,-71.3044285066583,
-81.1488399089191,-28.4921694559285,-50.792245141314,-89.1781760601729,
-17.212336630166,-93.1092705526953,-72.5010697091015,
-91.3555597791678,-99.1801088713358,-98.8238157487936,
-74.7895339241566,-89.1254655860885,-97.5625074750269,
-94.3452831311089,-87.3329588391988,-68.1886735569316,-89.2151012094226,
-96.6129855413483,-84.601128797229,-86.5203725025342,
-99.766749307876,-83.4141224683991,-87.3270904766443,
-98.4856524683997,-66.2048859641836,-87.0444585324901,-99.2891940088702,
-80.4086573939474,-26.2716449602661,-82.5277110810156,
-41.9333006438959,-94.3673106679853,-67.4580515739835,
-78.8029044763605,-87.6402523638039,-61.2759227693545,
-61.8068232395229,-58.200936563508,-53.2894032085419,-81.7793488663102,
-58.2186030228637,-70.6391489402592,-84.2803286740207,
-96.9256883885391,-69.177188268645,-69.4711477795203,
-63.7610046482861,-89.7772036333302,-94.8341000128459,-95.0187727103968,
-77.9697691755919,-90.7013355026706,-48.2716516570092,
-96.5664276058306,-50.3869994919628,-83.2806857193812,
-29.075942795378,-89.6329412046582,-95.3779913930632,
-85.6915761258382,-61.1509294825158,-80.4638349419718,-81.5304951649641,
-96.3314987443859,-88.3750722687825,-35.4249949479525,
-91.7023870977315,-99.2683326580896,-92.0885510129918,
-91.917002426524,-82.1136570807706,-96.4439473279938,-80.6907907077332,
-60.5454276771924,-74.517951764598,-80.4913002730443,
-73.1009185170308,-17.7752757087592,-52.7707256747252,
-91.2169382496002),
2021 = c(-64.8922439073551,-95.6548890102796,
-99.1968687600502,-69.9111172626283,-87.8213746953304,
-77.4864987579406,-33.5569646860987,-71.0263584886917,
-89.4931343176046,-98.5833495915478,-91.4044955302372,-97.643060873544,
-95.6041157473346,-77.0151941926244,-63.505499062754,
-96.5831406059174,-94.802486827916,-92.2170034531668,
-85.630234194672,-61.1506080368258,-90.006016398183,-88.4792099827229,
-92.5534611205697,-64.5239314941557,-92.526924051874,
-68.3038666761025,-93.9607110264119,-96.6070788052698,
-95.2467339768508,-72.5894073586287,-93.4075359624267,
-82.2460187908406,9.07431646305992,-89.4085579473715,-78.4710420633002,
-89.99514030291,-98.4544975250756,-97.3366805244598,
-78.6111305195944,-79.1513799849779,-99.8465053724655,NA,
-75.4520071619924,-66.0151976503083,-65.3343161588875,
-74.1583368053053,-82.9134751284754,-83.3809810662339,-99.7592573957332,
-84.2955113366379,-87.4061577499737,-98.6767969664322,
-55.7521055196883,-51.9094715118172,-98.5749266631065,
-79.3733949155146,-15.3495613975684,-79.5416773607996,-76.390284488197,
-89.9330197435627,-69.5338195289788,-76.2290664324723,
-87.5736038705322,-61.7999137094464,-58.3058337020952,
-61.1606869083645,-34.1657577547978,-87.9465990889762,
-46.2984841368038,-66.5173079894916,-86.0451729735691,-97.0017933512482,
-61.8277326072112,-69.6787046305625,-73.9032249084861,
-39.3850772362133,-83.978747250572,-83.1285130723916,
-77.6487811856004,-91.8724699591942,-56.8468372745294,-77.37972703087,
-50.4863176204071,19.99866910446,-98.7642634886819,
-97.1371330066335,-97.1682338971084,-32.3103334083127,
-75.2903912666234,-67.9302161503431,-21.3742167628881,-96.3792216143565,
-70.8013412722699,-46.0782311922542,-85.8698779704895,
-79.0040865719559,-93.6782958314544,-74.6925603421937,
-82.0169511642073,-99.6043862487825,-86.4474703199176,
-56.6439638489159,-85.3715709868519,-88.0722932762498,-79.8949575045002,
-9.95900611990872,-75.9908257384204,-89.2836143774366)
)

Hello,

I'm not sure what exactly it is you want to do, but if you are just looking to find the min and max values by row, you can easily do this without the need for any packages like this:

#The data
myData = data.frame(
  check.names = FALSE,
  `2017` = c(
    -29.6765588373183,
    NA,-89.2790876524891,-99.5039287808624,-85.8044413319264,-81.6880304924956,-61.6649799233737,-95.9201077199282,-76.9212542696249,-52.8207862180081,
    -90.690884048419,-98.9861843471265,-95.3035792168285,-94.4460797686721,-66.8589951783467,-96.4280665380505,-94.3530923544558,-90.5172796372587,
    NA,-52.1448699910989
  ),
  `2018` = c(
    -64.1666519351693,-60.4951830138689,-88.3812908403072,-99.4453492319537,-85.1840785602037,-74.8927001146028,
    -67.50034588967,
    193.50555145579,-69.6587622123482,-62.9400750278181,-90.5364373853199,-98.8384040512051,-95.337321076871,-88.6414474547727,-66.8471045702422,-96.1168493115813,-94.478647451438,-91.3361487440683,-83.5182199275366,
    -50.538914624392
  ),
  `2019` = c(
    -52.5696389710396,-68.5859389725439,-98.9198841135791,-91.1648878522288,-85.4167659140072,-75.1679467628325,
    -74.3341913058782,-95.4214517281169,-69.5173581710415,-59.371765611653,-90.2105027064727,-97.996423458458,-93.6959663716509,-87.3466553517521,-64.7361516754657,-96.2646796178946,-94.6184425052621,-92.4227306411747,-82.9866576317588,-64.8114393147269
  ),
  `2020` = c(
    -60.497486599285,-74.2630607258552,-99.048975073046,-94.8555487419248,
    -87.35265797426,-78.6036965320539,-68.3986489048762,-44.0030164110681,-75.2033353611765,-63.707434822513,-91.179475911318,-97.8606571078287,-96.2395936327372,-87.1911279391413,-61.587412113665,-96.1577754075495,-94.4178148872262,-92.1399352140724,
    -83.888876357997,-40.0249325279527
  ),
  `2021` = c(
    -64.8922439073551,-95.6548890102796,-99.1968687600502,-69.9111172626283,-87.8213746953304,-77.4864987579406,
    -33.5569646860987,-71.0263584886917,-89.4931343176046,-98.5833495915478,-91.4044955302372,-97.643060873544,-95.6041157473346,-77.0151941926244,-63.505499062754,-96.5831406059174,-94.802486827916,-92.2170034531668,-85.630234194672,-61.1506080368258
  )
)

#Create column with min and max values for each row using apply
myData$rowMin = apply(myData, margin = 1, min, na.rm = T)
myData$rowMax = apply(myData, margin = 1, max, na.rm = T)

head(myData)
#>        2017      2018      2019      2020      2021    rowMin    rowMax
#> 1 -29.67656 -64.16665 -52.56964 -60.49749 -64.89224 -64.89224 -29.67656
#> 2        NA -60.49518 -68.58594 -74.26306 -95.65489 -95.65489 -60.49518
#> 3 -89.27909 -88.38129 -98.91988 -99.04898 -99.19687 -99.19687 -88.38129
#> 4 -99.50393 -99.44535 -91.16489 -94.85555 -69.91112 -99.50393 -69.91112
#> 5 -85.80444 -85.18408 -85.41677 -87.35266 -87.82137 -87.82137 -85.18408
#> 6 -81.68803 -74.89270 -75.16795 -78.60370 -77.48650 -81.68803 -74.89270

Created on 2023-06-29 with reprex v2.0.2

By using apply, you can perform action on every row (margin = 1) or column (margin = 2). In this case we apply by row and run the min / max function with na.rm = T to ensure that NA values are ignored. The results are added as a new column

Hope this helps,
PJ

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.