[Rd] [[.data.frame oddity

William Dunlap wdunlap at tibco.com
Mon Apr 4 17:54:19 CEST 2011


data.frame[[row,col]] unclasses its output.  Is that proper?

> d <- data.frame(num=1:4,
          fac=factor(letters[11:14],levels=letters[1:15]),
          date=as.Date("2011-04-01") + (0:3),
          lt=as.POSIXlt("2011-04-01") + (0:3)*1e5,
          pv=package_version(c("1.2-3", "4.5","6.7", "8.9-10")))
> d
  num fac       date                  lt     pv
1   1   k 2011-04-01 2011-04-01 00:00:00  1.2.3
2   2   l 2011-04-02 2011-04-02 03:46:40    4.5
3   3   m 2011-04-03 2011-04-03 07:33:20    6.7
4   4   n 2011-04-04 2011-04-04 11:20:00 8.9.10
> str(d)
'data.frame':   4 obs. of  5 variables:
 $ num : int  1 2 3 4
 $ fac : Factor w/ 15 levels "a","b","c","d",..: 11 12 13 14
 $ date:Class 'Date'  num [1:4] 15065 15066 15067 15068
 $ lt  : POSIXct, format: "2011-04-01 00:00:00" "2011-04-02 03:46:40"
...
 $ pv  :List of 4
  ..$ :Classes 'package_version', 'numeric_version'  hidden list of 1
  .. ..$ : int  1 2 3
  ..$ :Classes 'package_version', 'numeric_version'  hidden list of 1
  .. ..$ : int  4 5
  ..$ :Classes 'package_version', 'numeric_version'  hidden list of 1
  .. ..$ : int  6 7
  ..$ :Classes 'package_version', 'numeric_version'  hidden list of 1
  .. ..$ : int  8 9 10
  ..- attr(*, "class")= chr  "package_version" "numeric_version"

> d[[1,1]]
[1] 1
> d[[1,2]] # d[1,2] gives factor("k",levels=letters[1:15])
[1] 11
> d[[1,3]] # d[1,3] gives as.Date("2011-04-01")
[1] 15065
> d[[1,4]]  # d[1,4] gives as.POSIXct("2011-04-01")
[1] 1301641200
> d[[1,5]] # d[1,5] gives package_version("1.2.3")
[1] 1 2 3

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 



More information about the R-devel mailing list