Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I have two df's: one is based on session and the other one on the client level. In the client level df I just have to sum the number of sessions done by a client in the session df.

The two dataframes are:

          df1                                df2

Sessionid     Client id                    Clientid     

   1              1                           1                 

   2              1                           2                 

   3              2                  

   4              2                  

   5              2    

The desired output is:

          df1                                df2

Sessionid     Client id             Clientid     Number_of_sessions

   1              1                    1                 2

   2              1                    2                 3

   3              2                  

   4              2                  

   5              2    

1 Answer

0 votes
by (108k points)

For achieving that in R programming, there are two ways. Either you can use the dplyr package:

df %>%

  group_by(Clientid) %>%

  summarise(Number_of_sessions = n())

# Clientid Number_of_sessions

# <int>              <int>

# 1        1           2

# 2        2           3

Or you can use aggregate() in the R base:

aggregate(Sessionid ~ Clientid, df, length)

The data frame is:

structure(list(Sessionid = 1:5, Clientid = c(1L, 1L, 2L, 2L, 

2L)), class = "data.frame", row.names = c(NA, -5L)) -> df

Browse Categories

...