Puzzle or bug with matrix indexing

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

Puzzle or bug with matrix indexing

David Hugh-Jones-3
I'm not sure why this is happening:

tmp <- data.frame(
  a = letters[1:2],
  b=c(TRUE, FALSE),
  stringsAsFactors = FALSE
)
idx <- matrix(c(1, 2, 2, 2), 2, byrow = TRUE)
tmp[idx]

[1] " TRUE" "FALSE"

Notice there is a space before the TRUE: " TRUE".

This space isn't happening purely because of coercion:

c("blah", TRUE, FALSE)
[1] "blah"  "TRUE"  "FALSE"

No space.

Obviously:
as.logical(tmp[idx])
[1]    NA FALSE

which is why I think this might be a bug.

David

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Puzzle or bug with matrix indexing

Berry, Charles


> On Aug 4, 2018, at 6:55 AM, David Hugh-Jones <[hidden email]> wrote:
>
> I'm not sure why this is happening:
>
> tmp <- data.frame(
>  a = letters[1:2],
>  b=c(TRUE, FALSE),
>  stringsAsFactors = FALSE
> )
> idx <- matrix(c(1, 2, 2, 2), 2, byrow = TRUE)
> tmp[idx]
>
> [1] " TRUE" "FALSE"
>

From ?"[.data.frame"

Extract.data.frame {base}
.
.
.
Matrix indexing (x[i] with a logical or a 2-column integer matrix i) using [ is not recommended. For extraction, x is first coerced to a matrix.

[...]

Thus, something like

        as.matrix(tmp)

happens converting every column to character. Dig deeper by reading

?matrix

and see the paragraph on `as.matrix' under `Details'.

HTH,

Chuck
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Puzzle or bug with matrix indexing

David Hugh-Jones-3
Yup, I worked it out in time... for future reference, as.matrix calls
`format` on logicals, converting them to the form seen.

On Sat, 4 Aug 2018 at 17:53, Berry, Charles <[hidden email]> wrote:

>
>
> > On Aug 4, 2018, at 6:55 AM, David Hugh-Jones <[hidden email]>
> wrote:
> >
> > I'm not sure why this is happening:
> >
> > tmp <- data.frame(
> >  a = letters[1:2],
> >  b=c(TRUE, FALSE),
> >  stringsAsFactors = FALSE
> > )
> > idx <- matrix(c(1, 2, 2, 2), 2, byrow = TRUE)
> > tmp[idx]
> >
> > [1] " TRUE" "FALSE"
> >
>
> From ?"[.data.frame"
>
> Extract.data.frame {base}
> .
> .
> .
> Matrix indexing (x[i] with a logical or a 2-column integer matrix i) using
> [ is not recommended. For extraction, x is first coerced to a matrix.
>
> [...]
>
> Thus, something like
>
>         as.matrix(tmp)
>
> happens converting every column to character. Dig deeper by reading
>
> ?matrix
>
> and see the paragraph on `as.matrix' under `Details'.
>
> HTH,
>
> Chuck
>
--
Sent from Gmail Mobile

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel