-1

I have a dataset with 2 columns: user and seconds. User is a categorical variable, with each user being assigned a number. A user can appear multiple times in the dataset. Seconds represent the amount of time spent on a page.

I have already used factor() so that R recognizes the user variable as categorical. However, I can't figure out how to find the minimum number of seconds for each user. that is, I can find the overall minimum number of seconds, but I can't find User 1's minimum or User 2's minimum. Any suggestions on how to proceed?

Data

user    seconds
1   35
1   30
1   25
1   20
2   15
1   15
2   10
1   10
2   5
1   5
divibisan
  • 11,659
  • 11
  • 40
  • 58
Shannon
  • 29
  • 1
  • 4
  • Possible duplicate of [Extract the maximum value within each group in a dataframe](https://stackoverflow.com/questions/25314336/extract-the-maximum-value-within-each-group-in-a-dataframe) – divibisan Sep 06 '18 at 20:18

3 Answers3

2

We can use data.table

library(data.table)
setDT(df1)[, list(Min=min(seconds)), by = user]

Or

library(dplyr)
df1 %>%
    group_by(user) %>%
    summarise(Min = min(seconds))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

It's basic content of R.

tapply(df1$seconds, df1$user, min)
# 1 2 
# 5 5 
Ven Yao
  • 3,680
  • 2
  • 27
  • 42
1

assume that your data is in a dataframe "x"

aggregate(seconds ~ user, data = x, FUN=min)

Ryan Castner
  • 954
  • 2
  • 10
  • 21