Curious subsetting behavior

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

Curious subsetting behavior

Sean Davis
I have a simple vector, called tmp that I want to subset based on another
vector called vec.  Everything works as expected except for below where the
subsetting returns something other than the original data.  Any ideas?

> vec <- c(1,2,3,4,5,59,60,27,32,21)
> tmp
 [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  7.0  8.0  8.1
9.0
[16]  9.1  9.2 10.0 10.1 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 18.1 19.0
20.0
[31] 20.1 21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 28.1 29.0 29.1 30.0 31.0
32.0
[46] 34.0 35.0 37.0 37.1 38.0 39.0 40.0 41.0 42.0 43.0 44.0 45.0 45.1 46.0
48.0
[61] 50.0
> tmp[-vec]
 [1]  3.0  3.1  4.0  5.0  5.1  6.0  7.0  8.0  8.1  9.0  9.1  9.2 10.0 10.1
11.0
[16] 13.0 14.0 15.0 16.0 17.0 18.1 19.0 20.0 20.1 22.0 23.0 24.0 25.0 26.0
27.0
[31] 28.0 28.1 29.0 29.1 30.0 31.0 32.0 34.0 35.0 37.0 37.1 38.0 39.0 40.0
41.0
[46] 42.0 43.0 44.0 45.0 45.1 50.0
> vec <- which(!is.na(MA.exp$targets$Grade))
> vec
 [1]  1  3  8  9 11 12 13 15 18 21 22 23 24 25 26 27 29 30 32 33 34 35 36 37
38
[26] 39 41 43 44 45 46 47 48 51 52 53 54 55 56 57 59 60 61
> str(vec)
 int [1:43] 1 3 8 9 11 12 13 15 18 21 ...
> tmp[1:20]
 [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  7.0  8.0  8.1
9.0
[16]  9.1  9.2 10.0 10.1 11.0

Everything above is as expected.  However, look at the output of tmp[vec]
below.  Why are the values of tmp incorrect?  What am I missing?

> tmp[vec]
 [1]  1  2  4  5  6  7  8  9 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27
[26] 28 29 30 31 32 34 35 37 39 40 41 42 43 44 45 46 48 50
> sessionInfo()
Version 2.3.0 Under development (unstable) (2006-01-04 r36984)
powerpc-apple-darwin8.3.0

attached base packages:
[1] "tools"     "methods"   "stats"     "graphics"  "grDevices" "utils"
[7] "datasets"  "base"

other attached packages:
geneplotter    annotate     Biobase      gplots       gdata      gtools
    "1.9.5"     "1.9.2"     "1.9.4"     "2.0.0"     "2.0.0"     "2.0.0"
  RdbiPgSQL   RdbiPgSQL        Rdbi     seanlib       limma
    "1.0.9"     "1.0.9"     "1.0.4"       "1.0"     "2.4.9"


Thanks,
Sean


        [[alternative HTML version deleted]]

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

Re: Curious subsetting behavior

Liaw, Andy
Just because R doesn't print the extraneous ".0" you think the result is
wrong?  

Andy

From: Sean Davis

>
> I have a simple vector, called tmp that I want to subset
> based on another vector called vec.  Everything works as
> expected except for below where the subsetting returns
> something other than the original data.  Any ideas?
>
> > vec <- c(1,2,3,4,5,59,60,27,32,21)
> > tmp
>  [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  
> 7.0  8.0  8.1 9.0 [16]  9.1  9.2 10.0 10.1 11.0 12.0 13.0
> 14.0 15.0 16.0 17.0 18.0 18.1 19.0 20.0 [31] 20.1 21.0 22.0
> 23.0 24.0 25.0 26.0 27.0 28.0 28.1 29.0 29.1 30.0 31.0 32.0
> [46] 34.0 35.0 37.0 37.1 38.0 39.0 40.0 41.0 42.0 43.0 44.0
> 45.0 45.1 46.0 48.0 [61] 50.0
> > tmp[-vec]
>  [1]  3.0  3.1  4.0  5.0  5.1  6.0  7.0  8.0  8.1  9.0  9.1  
> 9.2 10.0 10.1 11.0 [16] 13.0 14.0 15.0 16.0 17.0 18.1 19.0
> 20.0 20.1 22.0 23.0 24.0 25.0 26.0 27.0 [31] 28.0 28.1 29.0
> 29.1 30.0 31.0 32.0 34.0 35.0 37.0 37.1 38.0 39.0 40.0 41.0
> [46] 42.0 43.0 44.0 45.0 45.1 50.0
> > vec <- which(!is.na(MA.exp$targets$Grade))
> > vec
>  [1]  1  3  8  9 11 12 13 15 18 21 22 23 24 25 26 27 29 30 32
> 33 34 35 36 37 38 [26] 39 41 43 44 45 46 47 48 51 52 53 54 55
> 56 57 59 60 61
> > str(vec)
>  int [1:43] 1 3 8 9 11 12 13 15 18 21 ...
> > tmp[1:20]
>  [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  
> 7.0  8.0  8.1 9.0 [16]  9.1  9.2 10.0 10.1 11.0
>
> Everything above is as expected.  However, look at the output
> of tmp[vec] below.  Why are the values of tmp incorrect?  
> What am I missing?
>
> > tmp[vec]
>  [1]  1  2  4  5  6  7  8  9 10 12 13 14 15 16 17 18 19 20 21
> 22 23 24 25 26 27 [26] 28 29 30 31 32 34 35 37 39 40 41 42 43
> 44 45 46 48 50
> > sessionInfo()
> Version 2.3.0 Under development (unstable) (2006-01-04
> r36984) powerpc-apple-darwin8.3.0
>
> attached base packages:
> [1] "tools"     "methods"   "stats"     "graphics"  
> "grDevices" "utils"
> [7] "datasets"  "base"
>
> other attached packages:
> geneplotter    annotate     Biobase      gplots       gdata  
>    gtools
>     "1.9.5"     "1.9.2"     "1.9.4"     "2.0.0"     "2.0.0"  
>   "2.0.0"
>   RdbiPgSQL   RdbiPgSQL        Rdbi     seanlib       limma
>     "1.0.9"     "1.0.9"     "1.0.4"       "1.0"     "2.4.9"
>
>
> Thanks,
> Sean
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
>
>

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

Re: Curious subsetting behavior

Sean Davis



On 3/2/06 3:25 PM, "Liaw, Andy" <[hidden email]> wrote:

> Just because R doesn't print the extraneous ".0" you think the result is
> wrong?

Time to go home....

Thanks Andy.

Sean

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

do.call help

Andy Bunn
In reply to this post by Liaw, Andy
Hello all:

I have a character variable (foo) that contains the names of some numeric
variables. For my application, I'd like to cbind the numeric variables and
calculate the row mean using the character variable. I think I do this using
do.call but the function to call using do.call is eluding me! Example below
and help appreciated.
TIA,
-Andy


x1<-x2<-x3<-x4<-rnorm(10)
foo <- c("x1","x2","x3","x4")

# what I want...
rowMeans(cbind(x1,x2,x3,x4))

bar <- function(aCharVaraible) {
    cbind(stuff goes here)
    rowMeans(stuff goes here)
}
# how I want to get it...
do.call("bar",foo)

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

Re: do.call help

Sundar Dorai-Raj


Andy Bunn wrote:

> Hello all:
>
> I have a character variable (foo) that contains the names of some numeric
> variables. For my application, I'd like to cbind the numeric variables and
> calculate the row mean using the character variable. I think I do this using
> do.call but the function to call using do.call is eluding me! Example below
> and help appreciated.
> TIA,
> -Andy
>
>
> x1<-x2<-x3<-x4<-rnorm(10)
> foo <- c("x1","x2","x3","x4")
>
> # what I want...
> rowMeans(cbind(x1,x2,x3,x4))
>
> bar <- function(aCharVaraible) {
>     cbind(stuff goes here)
>     rowMeans(stuff goes here)
> }
> # how I want to get it...
> do.call("bar",foo)
>

How about:

set.seed(42)
x1 <- x2 <- x3 <- x4 <- rnorm(10)
foo <- c("x1","x2","x3","x4")
bar <- function(x) {
   y <- do.call("cbind", lapply(x, get))
   rowMeans(y)
}

do.call("bar", list(foo))

HTH,

--sundar

______________________________________________
[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