r - What does the dplyr period character "." reference? -
what period .
reference in following dplyr code?:
(df <- as.data.frame(matrix(rep(1:5, 5), ncol=5))) # v1 v2 v3 v4 v5 # 1 1 1 1 1 1 # 2 2 2 2 2 2 # 3 3 3 3 3 3 # 4 4 4 4 4 4 # 5 5 5 5 5 5 dplyr::mutate_each(df, funs(. == 5)) # v1 v2 v3 v4 v5 # 1 false false false false false # 2 false false false false false # 3 false false false false false # 4 false false false false false # 5 true true true true true
is shorthand "all columns"? .
specific dplyr syntax or general r syntax (as discussed here)?
also, why following code result in error?
dplyr::filter(df, . == 5) # error: object '.' not found
the dot used within dplyr (not exclusively) in mutate_each
, summarise_each
, do
. in first 2 (and se counterparts) refers columns functions in funs
applied. in do
refers (potentially grouped) data.frame can reference single columns using .$xyz
reference column named "xyz".
the reasons cannot run
filter(df, . == 5)
is because a) filter
not designed work multiple columns mutate_each
example , b) need use pipe operator %>%
(originally magrittr
).
however, use function rowsums
inside filter
when combined pipe operator %>%
:
> filter(mtcars, rowsums(. > 5) > 4) error: objekt '.' not found > mtcars %>% filter(rowsums(. > 5) > 4) %>% head() lm cyl disp hp drat wt qsec vs gear carb 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 3 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 4 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 5 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 6 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
you should take @ magrittr files:
library(magrittr) help("%>%")
from page:
placing lhs elsewhere in rhs call want lhs rhs call @ position first. purpose can use dot (.) placeholder. example,
y %>% f(x, .)
equivalentf(x, y)
,z %>% f(x, y, arg = .)
equivalentf(x, y, arg = z)
.using dot secondary purposes often, attribute or property of lhs desired in rhs call in addition value of lhs itself, e.g. number of rows or columns. valid use dot placeholder several times in rhs call, design behavior different when using inside nested function calls. in particular, if placeholder used in nested function call, lhs placed first argument! reason in use-cases produces readable code. example,
iris %>% subset(1:nrow(.) %% 2 == 0)
equivalentiris %>% subset(., 1:nrow(.) %% 2 == 0)
more compact. possible overrule behavior enclosing rhs in braces. example,1:10 %>% {c(min(.), max(.))}
equivalentc(min(1:10), max(1:10))
.
Comments
Post a Comment