Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

r - Going from dplyr to base: create a data frame of the first and last index for each level of a variable

Asking how to go from dplyr to base may be a weird ask, especially since I love the tidyverse, but I think because I learned the tidyverse first, my grasp of base is far from masterful, and I need a base solution because the package I'm helping to develop doesn't want any tidyverse dependencies

Data (there are many more columns, but abbreviated for reprex sake):

sample.df <- tibble(batch = rep(c(1,2,3), c(4,5,6)))

Desire base equivalent of:

sample.df %>%
  mutate(rowid = row_number()) %>%
  group_by(batch) %>%
  summarize(idx_b = min(rowid),
            idx_e = max(rowid))

# A tibble: 3 x 3
# Groups:   batch [3]
  batch idx_b idx_e
  <dbl> <int> <int>
1     1     1     4
2     2     5     9
3     3    10    15
question from:https://stackoverflow.com/questions/65892745/going-from-dplyr-to-base-create-a-data-frame-of-the-first-and-last-index-for-ea

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We create a sequence column in the data, use aggregate to get the range or min/max and convert the matrix column to regular data.frame column with do.call

out <- do.call(data.frame, aggregate(rowid ~ batch,
   transform(sample.df, rowid = seq_len(nrow(sample.df))), 
      FUN = function(x) c(b = min(x), e = max(x))))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...