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
:(df, 1, nrow(df))
to do? You're mixing scalars and data frames in a way that is not at all obvious to resolve. – Elihuseq_len(data.frame(1:10) %>% nrow(.))
– Sneeze:(df, 1, nrow(df))
.:
works likei:j
? – Kelby:
is a function. You've supplied two arguments,1
andnrow(.)
. The pipe supplies a third,data.frame(1:10)
, which is inserted in the first position, resulting in a call with three parameters to a function which expects only two. Hence the error. – Elihuseq
is an option for you:data.frame(1:10) %>% {seq(1, nrow(.))}
– Axiology