[R] A potential bug for paste() ?

Douglas Bates bates at stat.wisc.edu
Thu May 7 18:18:56 CEST 2009


On Thu, May 7, 2009 at 9:56 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> It probably has less to do with paste() than with Theoph, but since
> we have no idea what that might be, it's hard to tell.

> See the bit about "reproducible example", please.

Well, actually Theoph is one of the datasets in the (required)
datasets package.  It happens to be data from a pharmacokinetics
experiment so it is natural that PK/PD scientist would think of it.

> find("Theoph")
[1] "package:datasets"
> str(Theoph)
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and
'data.frame':	132 obs. of  5 variables:
 $ Subject: Ord.factor w/ 12 levels "6"<"7"<"8"<"11"<..: 11 11 11 11
11 11 11 11 11 11 ...
 $ Wt     : num  79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 ...
 $ Dose   : num  4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 ...
 $ Time   : num  0 0.25 0.57 1.12 2.02 ...
 $ conc   : num  0.74 2.84 6.57 10.5 9.66 8.58 8.36 7.47 6.89 5.94 ...
 - attr(*, "formula")=Class 'formula' length 3 conc ~ Time | Subject
  .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
 - attr(*, "labels")=List of 2
  ..$ x: chr "Time since drug administration"
  ..$ y: chr "Theophylline concentration in serum"
 - attr(*, "units")=List of 2
  ..$ x: chr "(hr)"
  ..$ y: chr "(mg/l)"

> On Thu, May 7, 2009 at 10:38 AM, Jun Shen <jun.shen.ut at gmail.com> wrote:
>> Hi, everyone,
>>
>> Try the following command to see if you get TRUE or FALSE. I get FALSE on a
>> unix platform but TRUE on Windows. Any comment?
>>
>> all(paste(Theoph[1],Theoph[2])==paste(Theoph[[1]],Theoph[[2]]))

There is a difference between Theoph[1] and Theoph[[1]].  The Theoph
object is a data frame.  Whenever you use the single bracket extractor
you will get another data frame.

> str(Theoph[1])
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and
'data.frame':	132 obs. of  1 variable:
 $ Subject: Ord.factor w/ 12 levels "6"<"7"<"8"<"11"<..: 11 11 11 11
11 11 11 11 11 11 ...

A data frame is a special type of a list and, in general, this applies
to any list - single bracket extraction applied to a list always
produces a list, even if it is a list of one element.

The double bracket extractor returns the element of the list, not a sublist.

> str(Theoph[[1]])
 Ord.factor w/ 12 levels "6"<"7"<"8"<"11"<..: 11 11 11 11 11 11 11 11 11 11 ...

I think of the distinction as being like the difference between an
element of a set (the "[[" operator) and a subset of size one (the "["
operator).




More information about the R-help mailing list