[R] Sum matrix by rows, conditional on value

David Winsemius dwinsemius at comcast.net
Tue Nov 29 08:16:56 CET 2011


On Nov 29, 2011, at 1:08 AM, Katrina Bennett wrote:

> I'd like to sum a matrix only where the matrix meets a specific  
> condition.
> The matrix has 365 rows and about 50,000 columns.

If you describe the meaning attached to the data if might help readers  
understand what the acceptable options might be. (Please read the  
Posting Guide.)

>
> str(cdem.mat.yr)
> num [1:365, 1:41772] -43.5 -48.4 -45.9 -38.4 -32 ...
>
> I'm having trouble replicating this because it is not working out  
> for me,
> so I'm unsure I can provide an solid working example (apologies).
>
> I would like to subset my matrix where the values are greater than  
> zero. I
> can do this easily using the following command to generate a matrix of
> TRUE/FALSE conditions.
>
> thaw.index <- subset(cdem.mat.yr > 0)

Are you hoping to subset particular rows?

If not, then you can make a copy and then set the negative values to NA

  thaw.mat <-cdem.mat.yr
  is.na(thaw.mat)<- thaw.mat < 0

and ...

  rowSums(thaw.mat, na.rm=TRUE)

>
> However, every time I then try to run apply, or rowSums to sum the  
> matrix
> rows using this index, I get back errors messages or the wrong answer.

I am guessing that your subset operation produced a vector and that  
apply or rowSums no longer makes any sense.

> There is a lot of values that meet this condition, so I know this  
> issue is
> with my formatting of the argument.
>
> i.e.
> rowSums(cdem.mat.yr == thaw.index)
>  [1] 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 0 0 0 0 0
> [66] 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 0 0 0 0 0
> [131] 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 0 0 0 0 0
> [196] 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 0 0 0 0 0
> [261] 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 0 0 0 0 0
> [326] 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
>
> thaw.index.sum <- apply(cdem.mat.yr[which(cdem.mat.yr > 0)], 1, sum)
> Error in apply(cdem.mat.yr[which(cdem.mat.yr > 0)], 1, sum) :
>  dim(X) must have a positive length
>
> This just provides me with one single value (I want the sum of all  
> the rows
> individually)
> sum(apply(cdem.mat.yr, 1, function(x) x>0))
>
> If I only run the apply over the entire matrix, this works but I can't
> subset it according to my condition.
>
> apply(cdem.mat.yr, 1, sum)
>  [1] -1834000.521 -2038684.652 -1932676.585 -1619369.113 -1353598.426
> -1190377.640 -1263541.796 -1438280.178 -1472440.385 -1714465.774
> [11] -1945920.377 -2163888.712 -1836656.208 -1772994.790 -1864650.604
> -1633144.043 -1580619.187 -1684046.620 -1769963.843 -1787676.116
> [21] -1643345.342 -1497455.795 -1580307.433 -1483559.628 -1531067.546
> -1557093.271 -1363626.528 -1160882.203 -1118893.494 -1352922.958
> [31] -1441715.250 -1539084.024 -1717835.433 -1806727.136 -1887120.912
> -1645721.673 -1310700.520 -1531408.644 -1582011.715 -1460538.996
> [41] -1192880.165 -1211111.092 -1190390.732 -1241594.368 -1180704.394
> -1025346.594 -1073581.060  -889662.991  -798735.050  -784632.707
> [51]  -874676.120  -957186.890 -1054610.980 -1067208.121 -1222859.353
> -1477327.599 -1635653.310 -1696308.770 -1473630.951 -1283105.867
> [61] -1061390.704  -811017.224  -875804.422  -851608.811  -948160.325
> -1440351.359 -1206523.958 -1143659.246 -1405071.144 -1421438.254
> [71] -1374929.105 -1336184.952 -1237185.588 -1082307.120 -1019742.616
> -958257.706  -888078.311  -790481.841  -821010.686  -907205.025
> [81]  -966761.676  -926937.928  -908394.310  -976085.444  -971934.490
> -703952.655  -521541.649  -625973.624  -743458.875  -631452.421
> [91]  -584709.631  -565843.210  -604252.152  -616885.977  -522011.655
> -576824.263  -726170.003  -822902.735  -897385.940  -668897.194
> [101]  -525227.323  -493291.723  -559480.809  -627790.133  -607923.974
> -535240.664  -346627.878  -343257.607  -287171.179  -324723.615
> [111]  -389052.208  -420393.385  -498589.819  -542040.688  -394442.745
> -183163.637  -126540.029  -186213.012  -179799.971  -364410.639
> [121]  -309555.880  -357052.251  -321362.137  -394878.460  -498785.071
> -309942.686  -276417.534  -337700.381  -304804.510  -238100.600
> [131]  -261210.843  -201821.616  -299377.673  -232015.614  -121752.676
> -154925.661  -145809.729    15840.738   145755.754   -33601.212
> [141]   -24323.630    35036.731    55156.441    48603.824    87203.646
> 139653.449   111722.558   101036.307   153884.464   153151.263
> [151]   112680.914   108730.812   110198.055   127087.033    77174.238
> -67632.638   -35129.976    56801.006     6712.631     8838.200
> [161]    40086.874   -29691.225   -55861.564     7561.504    91232.944
> 31752.447     1694.756   -43835.544     2522.883    41727.218
> [171]    26918.990   128692.011   114752.327   131455.862   127149.113
> 144686.214   160344.465   102204.088   130322.785    70392.818
> [181]   100384.523   210138.826   230235.443   211137.595   199770.103
> 167185.988   103951.789   125589.859   224791.286   302672.172
> [191]   268148.251   258709.018   263356.469   122460.767   103103.124
> 1996.530    55150.667   148763.608   188425.704   172693.650
> [201]   173253.653   101070.947   142112.846   112317.201   101550.125
> 157215.618   184009.183    60265.750    94310.493   123499.949
> [211]   174061.906   247635.961   268679.388   277766.454   307758.389
> 357310.030   178755.543   127887.604    99974.033   138429.524
> [221]   119383.635    49204.240    51923.431    68469.511    63187.005
> 214353.285   394362.626   238075.491    94333.586   -13273.666
> [231]   -79469.532   -40806.205   -76871.578   -49806.672     1228.690
> -38687.429   -55499.077  -102810.705   -83816.775   -60862.409
> [241]   -66168.008   -78153.236   -33324.097   -92072.137  -135488.177
> -179163.704  -112465.506   -15616.185    29229.741   -34415.330
> [251]   -32972.658    17278.772   -39965.022  -112357.551  -141587.923
> -105968.196  -109854.967  -133479.542  -135685.520  -173992.141
> [261]  -152909.182  -131791.656  -166530.917  -228831.447  -350692.928
> -418850.501  -495572.140  -539540.225  -632322.131  -728383.982
> [271]  -706613.842  -572388.522  -313981.672  -529208.500  -484367.815
> -396279.857  -424551.369  -363920.897  -357247.042  -338639.919
> [281]  -298365.860  -295808.318  -296616.571  -275798.300  -378353.975
> -448394.813  -517904.514  -665762.732  -706816.178  -702220.686
> [291]  -741409.378  -815699.314  -749468.808  -648206.337  -538353.298
> -507039.293  -545760.353  -752355.424  -781048.382  -700609.954
> [301]  -787629.865  -920471.910  -987297.058 -1101214.452 -1164491.033
> -1233146.792 -1187575.379  -967913.570  -830991.543 -1017500.681
> [311] -1250575.007 -1416678.263 -1402879.243 -1251974.831 -1023492.832
> -1026830.185 -1293833.488 -1516913.350 -1557436.391 -1718719.068
> [321] -1725813.094 -1658967.540 -1643884.838 -1849296.409 -1891710.750
> -1623420.016 -1286998.600 -1260085.087 -1381181.790 -1191308.082
> [331]  -977525.224 -1158107.546 -1270227.479  -930469.578  -526018.105
> -714733.479  -977507.860 -1207830.910  -991946.711  -769735.051
> [341]  -970481.838 -1224850.395 -1349246.203 -1428091.218 -1494887.500
> -1376951.938 -1367801.367 -1353437.568 -1361260.296 -1438702.417
> [351] -1502473.526 -1652947.018 -1767153.073 -1517986.198 -1273778.527
> -1009474.240 -1000104.286 -1061987.549  -975002.278  -945633.852
> [361] -1211242.890 -1419950.962 -1533983.821 -1551817.331 -1636464.444
>
>
> Thoughts?
>
> Thank you,
> Katrina
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list