From dennis.walvoort at wur.nl  Mon Mar 17 14:20:06 2008
From: dennis.walvoort at wur.nl (dennis.walvoort at wur.nl)
Date: Mon, 17 Mar 2008 14:20:06 +0100 (CET)
Subject: [Rd] Accessing vector elements by a factor (PR#10971)
Message-ID: <20080317132006.E907D282EFF6@mail.pubhealth.ku.dk>

Full_Name: Dennis Walvoort
Version: 2.6.2
OS: MS Windows XP
Submission from: (NULL) (137.224.252.10)


I've encountered a problem when extracting vector elements by a factor.

Let's create a (named) character vector v:

i <- 1:10
v <- letters[i]
names(v) <- i

Let iInt, iChr, and iFac denote integer, character, and factor indices
respectively:

iInt <- i
iChr <- as.character(i)
iFac <- factor(iChr)

Now, consider the following tests:

all.equal(v, v[iInt]) # TRUE
all.equal(v, v[iChr]) # TRUE
all.equal(v, v[iFac]) # FALSE
all.equal(v, v[as.character(iFac)]) # TRUE

The third test returns FALSE. It seems that the internal factor coding is used
to extract vector elements and not the factor levels:

all.equal(v[iFac], v[as.integer(iFac)]) # TRUE

Is this intended behaviour?


Kind regards,

Dennis Walvoort



Version:
 platform = i386-pc-mingw32
 arch = i386
 os = mingw32
 system = i386, mingw32
 status = 
 major = 2
 minor = 6.2
 year = 2008
 month = 02
 day = 08
 svn rev = 44383
 language = R
 version.string = R version 2.6.2 (2008-02-08)

Windows XP (build 2600) Service Pack 2

Locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

Search Path:
 .GlobalEnv, package:stats, package:graphics, package:grDevices, package:utils,
package:datasets, package:methods, Autoloads, package:base



[Rd] Accessing vector elements by a factor (PR#10971)
dennis.walvoort at wur.nl