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

  • Home
  • SEARCH
  • 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 3604990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:04:04+00:00 2026-05-18T21:04:04+00:00

I have a dataframe foo looking like this Date Return 1998-01-01 0.02 1998-01-02 0.04

  • 0

I have a dataframe “foo” looking like this

Date       Return
1998-01-01  0.02
1998-01-02  0.04
1998-01-03 -0.02
1998-01-04 -0.01
1998-01-05  0.02
...
1998-02-01  0.1
1998-02-02 -0.2
1998-02-03 -0.1
etc.

I would like to add to this dataframe a new column showing me the density value of the corresponding return. I tried:

foo$density <- for(i in 1:length(foo$Return)) density(foo$Return, 
from = foo$Return[i], to = foo$Return[i], n = 1)$y

But it didn’t work. I really have difficulty applying a “function” to each row. But maybe there is also another way to do it, not using density()?

What I essentially would like to do is to extract the fitted density values from density() to the returns in foo. If I just do plot(density(foo$Return)) it gives me the curve, however I would like to have the density values attached to the returns.

@Joris:

foo$density <- density(foo$Return, n=nrow(foo$Return))$y 

calculates something, however seems to return wrong density values.

Thank you for helping me out!
Dani

  • 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-18T21:04:05+00:00Added an answer on May 18, 2026 at 9:04 pm

    On second thought, forget about the density function, I suddenly realized what you wanted to do. Most density functions return a grid, so don’t give you the evaluation in the exact points. If you want that, you can eg use the sm package:

    require(sm)
    foo <- data.frame(Return=rpois(100,5))
    foo$density <- sm.density(foo$Return,eval.points=foo$Return)$estimate
    # the plot
    id <- order(foo$Return)
    hist(foo$Return,freq=F)
    lines(foo$Return[id],foo$density[id],col="red")
    

    If the number of different values is not that big, you can use ave() :

    foo$counts <- ave(foo$Return,foo$Return,FUN=length)
    

    If the purpose is to plot a density function, there’s no need to calculate it like you did. Just use

    plot(density(foo$Return))
    

    Or, to add a histogram underneath (mind the option freq=F)

    hist(foo$Return,freq=F)
    lines(density(foo$Return),col="red")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe with a column of integers that I would like to
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe and would like to calculate the correlation (with Spearman, data
I have a dataframe called ants detailing multiple entries per site, looks like this:
I would like to transform/modify the content of dataframe. Basically I have a dataframe
I have dataframe (return.monthly) of the form Date Return 2001-09-1 0.0404775 2001-10-1 -0.01771575 2001-11-1
I would like to apply some function on each row of a dataframe in
I have found this example on StackOverflow: var people = new List<Person> { new
I have a data.frame with entries like: variable importance order 1 foo 0.06977263 1
I have a dataframe: foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), df2

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.