only 0s may be mixed with negative subscripts

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

only 0s may be mixed with negative subscripts

Hasan Diwan-2
I'd like to get the sum of every other row in a data.frame. When I
actually set about doing this, I get the error in the subject line of
this message. A sample of my data is below, followed by the function
call that should give me the results I want:

> dput(head(sens2))
structure(list(Time = c(1328565067, 1328565067.05, 1328565067.1,
1328565067.15, 1328565067.2, 1328565067.25), Y = c(0.0963890795246276,
0.227296347215609, 0.240972698811569, 0.221208948983498, 0.230898231782485,
0.203282153087549), X = c(0.0245045248243853, 0.0835679411703579,
0.0612613120609633, 0.058568910563872, 0.0511868450318788, 0.0557714205674231
), rownumber = 1:6), .Names = c("Time", "Y", "X", "rownumber"
), row.names = c(NA, 6L), class = "data.frame")
> speedX <- sapply(sens2[sens2$rownumber %% 2 == 0,], function(row) { cumsum(c(sens2[row+1,3], sens2[row,3]))}, simplify=TRUE)
Error in xj[i] : only 0's may be mixed with negative subscripts
Help?
--
Sent from my mobile device
Envoyait de mon portable

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: only 0s may be mixed with negative subscripts

ilai-2
The function you posted runs without error (on these 6 lines), but
does not return anything that looks remotely like a sum, or cumsum of
anything. Can you clarify what you are trying to do? I assume by "sum
of every other row" you don't mean summing Time, X and Y for rows
1,3,5,..., ?

For the sum of sens2[c(1,3,5,...),] for every column (assuming no NA's
in the data) you could

 (1:nrow(sens2) %% 2) %*% as.matrix(sens2)

           Time       Y         X rownumber
[1,] 3985695201 0.56826 0.1369527         9

Hope this helps


On Mon, Feb 13, 2012 at 11:56 AM, Hasan Diwan <[hidden email]> wrote:

> I'd like to get the sum of every other row in a data.frame. When I
> actually set about doing this, I get the error in the subject line of
> this message. A sample of my data is below, followed by the function
> call that should give me the results I want:
>
>> dput(head(sens2))
> structure(list(Time = c(1328565067, 1328565067.05, 1328565067.1,
> 1328565067.15, 1328565067.2, 1328565067.25), Y = c(0.0963890795246276,
> 0.227296347215609, 0.240972698811569, 0.221208948983498, 0.230898231782485,
> 0.203282153087549), X = c(0.0245045248243853, 0.0835679411703579,
> 0.0612613120609633, 0.058568910563872, 0.0511868450318788, 0.0557714205674231
> ), rownumber = 1:6), .Names = c("Time", "Y", "X", "rownumber"
> ), row.names = c(NA, 6L), class = "data.frame")
>> speedX <- sapply(sens2[sens2$rownumber %% 2 == 0,], function(row) { cumsum(c(sens2[row+1,3], sens2[row,3]))}, simplify=TRUE)
> Error in xj[i] : only 0's may be mixed with negative subscripts
> Help?
> --
> Sent from my mobile device
> Envoyait de mon portable
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: only 0s may be mixed with negative subscripts

Hasan Diwan-2
On 13 February 2012 14:46, ilai <[hidden email]> wrote:
> The function you posted runs without error (on these 6 lines), but
> does not return anything that looks remotely like a sum, or cumsum of
> anything. Can you clarify what you are trying to do? I assume by "sum
> of every other row" you don't mean summing Time, X and Y for rows
> 1,3,5,..., ?

I'm trying to get a piecewise sum of every n rows in a given column in
this data set.
> For the sum of sens2[c(1,3,5,...),] for every column (assuming no NA's
> in the data) you could

I do format checking well before getting to this stage in the analysis.
>
>  (1:nrow(sens2) %% 2) %*% as.matrix(sens2)

That does not do what I want... Again, what it should return is a list
of the sum of the current and preceding row.
--
Sent from my mobile device
Envoyait de mon portable

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: only 0s may be mixed with negative subscripts

Rui Barradas
Hello,

>
> I'm trying to get a piecewise sum of every n rows in a given column in
> this data set.
>

Is this it?

# Result as a matrix, each column a piecewise cumsum
mat <- sapply(which(sens2$rownumber %% 2 == 0),
                function(row) cumsum(c(sens2[row-1, 3], sens2[row, 3])))

# The same but c() makes it a vector
vec <- c(sapply(which(sens2$rownumber %% 2 == 0),
                function(row) cumsum(c(sens2[row-1, "X"], sens2[row, "X"]))))

# See the results
data.frame(sens2, CumSum=vec)

If this is what you want, return to your original 'sapply' instruction and see that
you were passing entire rows to 'function(row)', not just row numbers.
It was too complicated.
(If it's not, sorry, but I missed the point.)

Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: only 0s may be mixed with negative subscripts

ilai-2
In reply to this post by Hasan Diwan-2
Like this ? sum every batch of n rows?

N <- 2
t(kronecker(diag(nrow(sens2)/N),rep(1,N))) %*% as.matrix(sens2)

           Time         Y         X rownumber
[1,] 2657130134 0.3236854 0.1080725         3
[2,] 2657130134 0.4621816 0.1198302         7
[3,] 2657130134 0.4341804 0.1069583        11

Check rownumber column: 1+2 = 3, 3+4 = 7, 5+6=11 ...

Or sum every 3 rows:
 N <- 3
 t(kronecker(diag(nrow(sens2)/N),rep(1,N))) %*% as.matrix(sens2 )
           Time         Y         X rownumber
[1,] 3985695201 0.5646581 0.1693338         6
[2,] 3985695202 0.6553893 0.1655272        15

Hope this helps (more...).

On Mon, Feb 13, 2012 at 4:04 PM, Hasan Diwan <[hidden email]> wrote:

> On 13 February 2012 14:46, ilai <[hidden email]> wrote:
>> The function you posted runs without error (on these 6 lines), but
>> does not return anything that looks remotely like a sum, or cumsum of
>> anything. Can you clarify what you are trying to do? I assume by "sum
>> of every other row" you don't mean summing Time, X and Y for rows
>> 1,3,5,..., ?
>
> I'm trying to get a piecewise sum of every n rows in a given column in
> this data set.
>> For the sum of sens2[c(1,3,5,...),] for every column (assuming no NA's
>> in the data) you could
>
> I do format checking well before getting to this stage in the analysis.
>>
>>  (1:nrow(sens2) %% 2) %*% as.matrix(sens2)
>
> That does not do what I want... Again, what it should return is a list
> of the sum of the current and preceding row.
> --
> Sent from my mobile device
> Envoyait de mon portable
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.