[R] hwo to subtract a child array from the big array?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Jan 14 08:54:47 CET 2013


Although I think I understand "every 10 numbers", I am not so sure how to interpret "subtract aa from AA" for a couple of reasons.

First, to decimate your data:
ab <- AA[(1:10)*10]

As for subtraction, you have not told us what variable aa is. If you meant variable ab, it has only 10 values while AA has 100 values. If you try to subtract ab, R will "recycle" values until you get 10 copies of ab.

If you are thinking of set difference, there is a setdiff function, but that will only work reliably with integer data (which you have here but may not in general). However, you may find reading the section on indexing in the Introduction to R document that ships with R to be very useful. In there it mentions the effect of using negative index values:

AA[-(1:10)*10]

giving you "the rest of the data".
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Jie Tang <totangjie at gmail.com> wrote:

>hi R users
>  I have a data set with the name AA
>AA<-1:100
>Now I want to get a child array from AA every 10 numbers
>e.g.
>ab =c(10,20,30,40,50,60,70,80,100)
>
>How could I subtract aa from AA?
>thank you .




More information about the R-help mailing list