public bigdata

%>% (R4DS 일부, help("%>%) 일부) 정리 본문

R programming

%>% (R4DS 일부, help("%>%) 일부) 정리

public bigdata 2019. 9. 2. 22:43

<dot>

https://stackoverflow.com/questions/35272457/what-does-the-dplyr-period-character-reference

 

What does the dplyr period character "." reference?

What does the period . reference in the 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...

stackoverflow.com

Placing lhs elsewhere in rhs call Often you will want lhs to the rhs call at another position than the first. For this purpose you can use the dot (.) as placeholder. For example, y %>% f(x, .) is equivalent to f(x, y) and z %>% f(x, y, arg = .) is equivalent to f(x, y, arg = z).
Using the dot for secondary purposes Often, some attribute or property of lhs is desired in the rhs call in addition to the value of lhs itself, e.g. the number of rows or columns. It is perfectly valid to use the dot placeholder several times in the rhs call, but by design the behavior is slightly different when using it inside nested function calls. In particular, if the placeholder is only used in a nested function call, lhs will also be placed as the first argument! The reason for this is that in most use-cases this produces the most readable code. For example, iris %>% subset(1:nrow(.) %% 2 == 0) is equivalent to iris %>% subset(., 1:nrow(.) %% 2 == 0) but slightly more compact. It is possible to overrule this behavior by enclosing the rhs in braces. For example, 1:10 %>% {c(min(.), max(.))} is equivalent to c(min(1:10), max(1:10)).