[R] rowSums - am I getting something wrong?

From: <Thomas.Salvesen_at_syngenta.com>
Date: Mon, 07 Mar 2011 08:08:53 +0100


I am trying to construct a data set with some sequences for example:

a = seq(0,1,0.1)

m = matrix(nrow = 1331, ncol = 3)

m[,1] = rep(a,121)
m[,2] = rep(a,11,each = 11)
m[,3] = rep(a,1,each = 121)

I realize that there may be better ways of doing this, but this approach demonstrates the problem I'm having.

I then want to get the sum of the rows and delete any row with a sum of greater than 1. But have a problem with rows containing any combination of the values 0.6, 0.3 and 0.1 as the sum of these is clearly 1, but a request for which rows have a sum greater than 1 will return rows with these values. Row 161 is the first row containing these values:

[161,] 0.6 0.3 0.1

which(rowSum(m)>1)

> [53] 119 120 121 132 142 143 152 153 154 161 162

As far as I can tell this only affects combinations of 0.6, 0.3 and 0.1 (though I haven't checked every value in the matrix)

If I try the following:

q=rowSums(m)
which(q>1)

>[53] 119 120 121 132 142 143 152 153 154 161 162

But if I add and subtract 1 from this:

q=q+1
q=q-1
which(q>1)

[53] 119 120 121 132 142 143 152 153 154 162

What exactly is going on here? I don't have the problem with other combinations (eg 0.7, 0.2, 0.1). I assume that there is something about the data format that I don't understand, but if I make a data frame of the matrix I found the same effect.

Any help would be great

Tom

message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.

        [[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. Received on Mon 07 Mar 2011 - 09:01:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Mar 2011 - 14:40:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive