Is there a way to get attributes of an R object that has been piped with %>%?

92 Views Asked by At

I use magrittr's piping (with %>%) a lot. Occasionally, I need to create a new column with attributes from the object being passed in. However, I haven't discovered a way to access those from within a piped statement.

For a simple example:

 df <- data.frame(x=c(1,2,3,4,5),
                 y=c(6,7,8,9,0)) %>%
  mutate(newcol=1:nrow(what goes here?))

In this example, I need to know the number of rows in the new data frame being created in the new column being defined, but currently if I need to do that, I start a new statement assignment with df$newcol <- 1:nrow(df). Then if I need to do something with newcol, I have to start a new assignment statement or piping chain.

Is there a way to do this from within the piping chain?

0

There are 0 best solutions below