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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:47:33+00:00 2026-05-28T16:47:33+00:00

R: how can I populate the rows of a data frame, in which each

  • 0

R: how can I populate the rows of a data frame, in which each row represents a day, with a single common value for each year?

I have a data frame consisting of a date column, a price column and then various other columns derived from those two columns. One of the columns calculates, for each day in a given year, the percentage change in the price from the beginning of that year (this is related to an earlier question).

I want to add a column that holds, for each day of a given year, the percentage change in the price for the whole of that year. So, if the price rose 10% from the first to the last day of 2009, the column for all the days of 2009 should hold the value 10% (or 0.1). If the price fell by 2% between the first and last days of 2010, the column for each day in 2010 should hold the value -0.02 and so on.

The code I have so far is:

require(lubridate)
require(plyr)
# generate data
set.seed(12345)
df <- data.frame(date=seq(as.Date("2009/1/1"), by="day", length.out=1115),price=runif(1115, min=100, max=200))
# remove weekend days
df <- df[!(weekdays(as.Date(df$date)) %in% c('Saturday','Sunday')),]
# add some columns for later
df$year <- as.numeric(format(as.Date(df$date), format="%Y"))
df$month <- as.numeric(format(as.Date(df$date), format="%m"))
df$day <- as.numeric(format(as.Date(df$date), format="%d"))
df$daythisyear <- as.numeric(format(as.Date(df$date), format="%j"))
df <- transform(df, doy = as.Date(paste(2000, month, day, sep="/")))
df <- ddply(df, .(year), transform, pctchg = ((price/price[1])-1))

I realise that I can get the annual (year-on-year) change by using another data frame, something like this:

df.yr <- ddply(df, .(year), function(x) (x[nrow(x),2]/x[1,2])-1)

…but I can’t work out how to add the figures for the years to a column in an existing data frame, particularly given that (if you are working with 4 years of data) there are only 4 rows, one for each year, compared to about 800 in the data frame of daily data used to derive those 4 rows – you get a mismatch.

It is straightforward to use a for loop starting at the last row of the data frame and moving back up the daythisyear column to achieve this (if daythisyear on current row is larger than daythisyear on the row below, you have a change in year, so take new value from that row to use in the column being added etc). Nevertheless, I feel sure there must be a more R-colloquial approach using an apply function or ddply, which I have so far studiously avoided tackling. So my question is:

Q. How do I calculate the annual change in the value of a column and then insert that value, as a new column, into every row for that year?

  • 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-05-28T16:47:34+00:00Added an answer on May 28, 2026 at 4:47 pm

    I’ve not yet converted to being a ddply user, preferring instead to use ave when it is the obvious solution. I suspect that this code would translate across:

     df$pctYrChng <- ave(df$price, df$year, FUN=function(x) tail(x,1)/head(x,1) - 1)
     unique(df$pctYrChng)
    #[1] -0.03259032 -0.05781901  0.35932519  0.04246669
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview which loads its data from sqlite database. Each row in
I have a class SomeClass, which can populate itself from a datarow in it's
I have simple table that has about 80 rows, which I populate dynamically using
I have table with two SelectOneMenu in row. I need populate data in second
I want to have/define a unique id for each data row in my Excel
I'm trying to normalize some data which I have in a data frame. I
I have a collection of objects, each of which holds a set of name-value
How can I populate multiple datagrids in flex with a single datasource that is
I have a table, in which I am adding new rows. After 6 rows
I am getting data from a database and which is used to populate the

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.