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 8217577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:30:16+00:00 2026-06-07T12:30:16+00:00

I have a data.frame representing frequency book sales for a set of authors over

  • 0

I have a data.frame representing frequency book sales for a set of authors over 25 weeks:

author       week_1 week_2 week_3 week_4 ...
author1      7      4      5          2
author2      3      6      18         5
author3      1      0      2          4
author4      0      1      1          2
author5      0      1      0          0

First, I want to use this data to build a new data frame, which shows the fraction of [currentWeek / previousWeek]. Something like this perhaps:

author       week_1 week_2  week_3 week_4 ...
author1      NA      0.57   1.25   0.2
author2      NA      2      3      0.28
author3      NA      0      2      2
author4      NA      1      1      2   
author5      NA      1      0      0   

(I would like to substitute zeros with 1s to avoid dividing by zero.)

Second, I want to run a quick iteration over all the rows, check for any triplets of adjacent weeks where sales for that authors have increased by 100% twice in two consecutive week-pairs, and report this in some kind of output table. Perhaps like this:

author  startTrendWeek endTrendWeek
author2 1              3
author3 2              4

Any ideas for how I could solve either of these in R?

  • 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-07T12:30:19+00:00Added an answer on June 7, 2026 at 12:30 pm

    Recreate your data:

    x <- read.table(text=
    "author       week_1 week_2 week_3 week_4 
    author1      7      4      5          2
    author2      3      6      18         5
    author3      1      0      2          4
    author4      0      1      1          2
    author5      0      1      0          0
                    ", header=TRUE)
    

    One line of code:

    cbind(x[1], t(apply(x[, -1], 1, function(xx)xx[-1]/xx[-length(xx)])))
    
       author    week_2 week_3    week_4
    1 author1 0.5714286   1.25 0.4000000
    2 author2 2.0000000   3.00 0.2777778
    3 author3 0.0000000    Inf 2.0000000
    4 author4       Inf   1.00 2.0000000
    5 author5       Inf   0.00       NaN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming in R, I have a data.frame with the first column representing the time
I have a data set representing data from a log file which shows users
I have a data frame that has a format like the following: Month Frequency
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with start dates and end dates, along with the
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame that's about ts1[100, 2000] in dimension as follows: >

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.