Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:15:20+00:00 2026-06-03T15:15:20+00:00

I am trying to define a function for ‘stickiness’ – a Business Analytics metric

  • 0

I am trying to define a function for ‘stickiness’ – a Business Analytics metric that measures user Engagement – and my function is returning a dataframe that is populated with unexpected data.

stickiness <- function(tdata) {
    require(plyr)
    mau_unique <- dlply(.data = tdata,
                        .variables = "dt",
                        .fun = function(x){unique(x$username)})
    dates_char <- names(mau_unique)
    dates_vector <- as.Date(dates_char[28:(length(dates_char))],
                            format = "%Y-%m-%d")
    output_df <- data.frame(dates_vector,
                            matrix(data = 0,
                                   nrow = length(dates_char) - 27,
                                   ncol = 3))
    colnames(output_df) <- c("Date", "DAU", "MAU", "Stickiness")
    for (i in 1:length(dates_vector)) {
        dt <- dates_vector[i]
        output_df[i, "DAU"] <- length(unlist(mau_unique[[as.character(dt)]][2]))
        set28 <- unique(unlist(lapply(X = mau_unique[i:(i + 27)], FUN = "[[", 2)))  
        output_df[i, "MAU"] <- length(set28)
        output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
    }
    return(output_df) 
}

The following is returned:

         Date DAU MAU Stickiness
1  2012-04-28   1  28 0.03571429
2  2012-04-29   1  28 0.03571429
3  2012-04-30   1  28 0.03571429
4  2012-05-01   1  28 0.03571429
5  2012-05-02   1  28 0.03571429
6  2012-05-03   1  28 0.03571429
7  2012-05-04   1  28 0.03571429
8  2012-05-05   1  28 0.03571429
9  2012-05-06   1  28 0.03571429
10 2012-05-07   1  28 0.03571429

I expected something like the following:

         Date   DAU    MAU Stickiness
1  2012-04-28 25000 250000 0.10000000
...  ...      ...   ...    ...
10 2012-05-07 27371 284114 0.09633809

I suspect that the problem is related to which environments I’m evaluating in.

UPDATED sample data:

> tdata
                 dt  username
    4236 2012-04-06 241343664
    3091 2012-04-06 306001012
    2936 2012-04-06 388682041
    5790 2012-04-05 235612064
    6763 2012-04-05  69650072
    3392 2012-04-06    617142
    7684 2012-04-05 189752749
    3904 2012-04-06 255852653
    7915 2012-04-05 182713266
    6107 2012-04-05 187675644

UPDATE working function (using Brian Diggs’s answer):

stickiness <- function(tdata) {
    require(plyr)
    mau_unique <- dlply(.data = tdata,
                        .variables = "dt",
                        .fun = function(x){unique(x$username)})
    dates_char <- names(mau_unique)
    dates_vector <- as.Date(dates_char[28:(length(dates_char))],
                            format = "%Y-%m-%d")
    output_df <- data.frame(dates_vector,
                            matrix(data = 0,
                                   nrow = length(dates_char) - 27,
                                   ncol = 3))
    colnames(output_df) <- c("Date", "DAU", "MAU", "Stickiness")
    for (i in 1:length(dates_vector)) {
        dt <- dates_vector[i]
        output_df[i, "DAU"] <- length((mau_unique[[as.character(dt)]])
        set28 <- unique(do.call(c, mau_unique[i:(i + 27)]))  
        output_df[i, "MAU"] <- length(set28)
        output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
    }
    return(output_df) 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:15:21+00:00Added an answer on June 3, 2026 at 3:15 pm

    Thanks for adding some sample data, but it is still not really reproducible since the function assumes the data spans at least 28 days (or rather, at least 28 unique dates).

    The problem, as near as I can figure, is inside your for loop. With your example data,

    > mau_unique
    $`2012-04-05`
    [1] 235612064  69650072 189752749 182713266 187675644
    
    $`2012-04-06`
    [1] 241343664 306001012 388682041    617142 255852653
    
    attr(,"split_type")
    [1] "data.frame"
    attr(,"split_labels")
              dt
    1 2012-04-05
    2 2012-04-06
    

    so in computing DAU, you pull a corresponding element from mau_unique. Working outward through your calculation of DAU with a dummy value for dt:

    > dt <- as.Date("2012-04-05")
    > dt
    [1] "2012-04-05"
    > as.character(dt)
    [1] "2012-04-05"
    > mau_unique[[as.character(dt)]]
    [1] 235612064  69650072 189752749 182713266 187675644
    > mau_unique[[as.character(dt)]][2]
    [1] 69650072
    > unlist(mau_unique[[as.character(dt)]][2])
    [1] 69650072
    > length(unlist(mau_unique[[as.character(dt)]][2]))
    [1] 1
    

    I don’t know how DAU should be calculated, but you always take the second username from the corresponding vector in mau_unique and take the length of that, which is why you always get 1. You are doing something similar for set28; I don’t know why you keep trying to pull the second element out.


    EDIT:

    Synthetically generated data is fine. That is a good way to create a lot of data in a small space, and with setting a random seed will allow everyone to work with the same data.

    set.seed(1234)
    tdata <- data.frame(dt = sample(seq(as.Date("2012-04-01"),
                                        as.Date("2012-04-30"),
                                        by = "day"),
                                    size = 10000,
                                    replace = TRUE),
                        username = sample(10000:10200,
                                          10000,
                                          replace = TRUE))
    

    Given you descriptions of DAU and MAU, I think your for loop should read: (the rest of the function is unchanged)

    for (i in 1:length(dates_vector)) {
        dt <- dates_vector[i]
        output_df[i, "DAU"] <- length(mau_unique[[as.character(dt)]])
        output_df[i, "MAU"] <- length(unique(unlist(mau_unique[i:(i+27)])))
        output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
    }
    

    given this, your stickiness is:

    > stickiness(tdata)
            Date DAU MAU Stickiness
    1 2012-04-28 156 201  0.7761194
    2 2012-04-29 168 201  0.8358209
    3 2012-04-30 152 201  0.7562189
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to define a function that would to that: var photoWidth =
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I am trying to define a function that will include a variable n where
I'm trying to define an anonymous function that calls a version of a function
So I'm trying to define a function g() that is like document.getElementById. The following
i'm trying to define a User registration class and this is the function i
I'm trying to Create user define $ function as i cannot use Jquery in
I am trying to figure out how to define a function that works on
when trying to define a function that would remove the largest subset of set
I'm trying to define a user function in vim to change the current color

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.