1

I take an extract from the data frame DF1 using certain index values ind, do some modifications to the extracted data frame, and then save it as DF2 using the %>% operator. Once I am done, I would like to renumber the entries in DF2:

DF2 <- DF1[ind,] %>% ...do some modifications... %>% row.names() <- NULL

My problem is that the row.names() <- NULL portion does not work. I have to use the code

row.names(DF2) <- NULL

afterwards to renumber the entries in DF2. However, I would like to do this as the last step before assigning the modified data frame to DF2. How can I modify the last portion to accomplish the renumbering from 1 to number of rows? I have tried using mutate, but could not get it to work. Thanks

Henrik
  • 61,039
  • 13
  • 131
  • 152
Ruan
  • 179
  • 9

1 Answers1

1

Using the `rownames<-`() assignment function.

library(magrittr)
d %>% `rownames<-`(NULL)
#   X1 X2 X3 X4
# 1  1  4  7 10
# 2  2  5  8 11
# 3  3  6  9 12

Data:

d <- structure(list(X1 = 1:3, X2 = 4:6, X3 = 7:9, X4 = 10:12), class = "data.frame", row.names = c("a", 
"b", "c"))
jay.sf
  • 46,523
  • 6
  • 46
  • 87