0

I have problem in hand where I have two vectors like below:

login_time  "08:00" "09:00" "09:00" "09:00" "09:00" "09:00" "09:00" "10:00" "11:00" "10:00" "10:00" "16:00" "17:00" "20:00" "21:00"
login_count 1     4     3     9     17    10    4     7     9     3     8     6     7     2     3    

Now whenever time in first row has multiple value I should sum up login count values and in the end I should have unique login_time value with summed up login count.

zx8754
  • 52,746
  • 12
  • 114
  • 209
leo11
  • 85
  • 4
  • 13

2 Answers2

2

We can use xtabs from base R.

xtabs(login_count~login_time)
#08:00 09:00 10:00 11:00 16:00 17:00 20:00 21:00 
#   1    47    18     9     6     7     2     3 
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You can do this using the package dplyr.

library(dplyr)
tmp_df <- data.frame(login_time = c("08:00","09:00","09:00","09:00","09:00","09:00","09:00","10:00","11:00","10:00","10:00","16:00","17:00","20:00","21:00"),
login_count  = c(1,4,3,9,17,10,4,7,9,3,8,6,7,2,3))

tmp_df %>%
    group_by(login_time) %>%
    summarise(total_logins = sum(login_count))

which produces:

Source: local data frame [8 x 2]

  login_time total_logins
      (fctr)        (dbl)
1      08:00            1
2      09:00           47
3      10:00           18
4      11:00            9
5      16:00            6
6      17:00            7
7      20:00            2
8      21:00            3
Alex
  • 15,186
  • 15
  • 73
  • 127