For some reason I wanted to run 1:nrow(.)
, where .
refers to the dataframe defined before. It is something like this:
library(tidyverse)
data.frame(1:10) %>%
1:nrow(.)
I get the error Error in :(., 1, nrow(.)) : 3 arguments passed to ':' which requires 2
. But data.frame(1:10) %>% nrow(.)
works as expected.
I tried to change rownames using {}
in a pipe but it doesnt work:
data.frame(1:10) %>%
{rownames(.) <- letters[1:nrow(.)]}
I expected the result to be data.frame(1:10, row.names= letters[1:10])
but it is not.
>Solution :
Put it in {
}
.
data.frame(1:10) %>% {1:nrow(.)}
# [1] 1 2 3 4 5 6 7 8 9 10
Or use:
data.frame(1:10) %>% nrow %>% 1:.
# [1] 1 2 3 4 5 6 7 8 9 10
You are using the pipe for a nested call. Pipe: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)).
And for the second part of the question, you just update the rownames. To see the result add a .
.
data.frame(1:10) %>% {rownames(.) <- letters[1:nrow(.)]; .}
# X1.10
#a 1
#b 2
#c 3
#d 4
#e 5
#f 6
#g 7
#h 8
#i 9
#j 10