0

I am looking for advice on how to add a column to a dataframe that for each factor level counts from 1 to n the number of factors in that level. Here is the example I am working with.

collatzRule <- function(m){
    if ( m %% 2 == 0) {
        return(m/2)
  } else {
        return(3*m + 1)
  }
}

collatz <- function(n, limit = 1000000000) {
  # collatz numbers will go in this vector
  numbers <- numeric(limit)
  # keep count of how many numbers we have made:
  counter <- 0
  while ( n > 1 & counter < limit) {
    # need to make a new number
    counter <- counter + 1
    # put the current number into the vector
    numbers[counter] <- n
    # make next Collatz number
    n <- collatzRule(n)
  }
  # find how many Collatz numbers we made:
  howMany <- min(counter, limit)
  # print them out:
  print(numbers[1:howMany])
}

datalist = list()

for (i in 2:100) {
    # ... make some data
    dat <- collatz(i) %>% as.data.frame()
    dat$i <- i %>% as.factor() # maybe you want to keep track of which iteration produced it?
    datalist[[i]] <- dat # add it to your list
}

big_data = do.call(rbind, datalist)

This produces a data frame that looks likes:

    .   i 
1   2   2       
2   3   3       
3   10  3       
4   5   3       
5   16  3       
6   8   3

I want to add a column that looks like this.

    .   i   x
1   2   2   1   
2   3   3   1   
3   10  3   2   
4   5   3   3   
5   16  3   4   
6   8   3   5

Any help would be greatly appreciated!

Best wishes,

  • This is surely a duplicate, but until I find the right question to mark as the dupe, try this: `big_data %>% group_by(i) %>% mutate(x = 1:n()) %>% ungroup()` – duckmayr Nov 27 '18 at 21:11
  • 2
    Possible duplicate of [Numbering rows within groups in a data frame](https://stackoverflow.com/questions/12925063/numbering-rows-within-groups-in-a-data-frame) – duckmayr Nov 27 '18 at 21:14
  • or in base R `big_data$x <- unlist(by(1:nrow(big_data), big_data$i, seq_along))` – Maurits Evers Nov 27 '18 at 22:06

0 Answers0