Closed renkun-ken closed 10 years ago
head.Pipe
and tail.Pipe
does not pass R CMD check
.
[ ]
, [[ ]]
, $<-
are defined for Pipe object so that the pipe continues with subsetting and extracting, as well as element setting. For example,
> Pipe(list(a=1))["a"]
$value : list
------
$a
[1] 1
> Pipe(list(a=1,b=2))[c("a","b")]
$value : list
------
$a
[1] 1
$b
[1] 2
> Pipe(list(a=1,b=2))[["a"]]
$value : numeric
------
[1] 1
> Pipe(mtcars)["mpg"]$head()
$value : data.frame
------
mpg
Mazda RX4 21.0
Mazda RX4 Wag 21.0
Datsun 710 22.8
Hornet 4 Drive 21.4
Hornet Sportabout 18.7
Valiant 18.1
> Pipe(mtcars)$as.data.table()[,mpg]
$value : numeric
------
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
It works with data.table
too.
> z <- data.table(x=1:3,y=rnorm(3),key = "x")
> p <- Pipe(z)
> p[J(1)]
$value : data.table data.frame
------
x y
1: 1 0.1901827
```rconsole
> p[J(1), y:=0.5]
$value : data.table data.frame
------
x y
1: 1 0.5000000
2: 2 1.0253236
3: 3 -0.6331669
Element setting also works.
> p$y <- 1:3
> p
$value : data.table data.frame
------
x y
1: 1 1
2: 2 2
3: 3 3
This allows direct call of subsetting and extracting, for example,
Pipe(mtcars)$
as.data.table()[mpg <= mean(mpg)]$
.(lm(mpg ~ wt + cyl, data = .))$
summary()$
.(coefficients)
$value : matrix
------
Estimate Std. Error t value Pr(>|t|)
(Intercept) 31.928632 4.0100333 7.962186 9.127888e-07
wt -2.183321 0.6536109 -3.340398 4.472574e-03
cyl -1.012133 0.5699081 -1.775959 9.602413e-02
For example,
head()
,tail()
, etc. should directly work on$value
rather thanPipe
environment.