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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:24:59+00:00 2026-06-17T13:24:59+00:00

I have two related questions — I’m trying to learn R properly, so I’m

  • 0

I have two related questions — I’m trying to learn R properly, so I’m doing some homework problems from an R course. They have us writing a function to return a vector of correlations:

example.function <- function(threshold = 0) {
  example.vector <- vector()
  example.vector <- sapply(1:30, function(i) {
    complete.record.count <- # ... counts the complete records in each of the 30 files.
    ## Cutting for space and to avoid giving away answers.
    ## a few lines get the complete records in each 
    ## file and count them. 
    if(complete.record.count > threshold) {
      new.correlation <- cor(complete.record$val1, complete.record$val2)
      print(new.correlation)
      example.vector <- c(new.correlation, example.vector)
    }  
  })
  # more null value handling#
  return(example.vector)
}

As the function runs it prints the correlation value to stdout. The values it prints are accurate to six decimal points. So I know I’m getting a good value for new.correlation. The vector that is returned doesn’t include those values. Instead, it is whole numbers in sequence.

> tmp <- example.function()
> head(tmp)
[1] 2 3 4 5 6 7

I can’t figure out why sapply is pushing integers into the vector? What am I missing here?

I actually don’t understand the core structure, which is more or less:

some.vector <- vector()
some.vector <- sapply(range, function(i) {
  some.vector <- c(new.value,some.vector)
}

that seems awfully un-R-like in its redundancy. Tips?

  • 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-17T13:25:00+00:00Added an answer on June 17, 2026 at 1:25 pm

    If you use sapply you don’t need to create the vector yourself and you don’t need to grow it (sapply takes care of all that). You probably want something like this:

    example.function <- function(threshold = 0) {
      example.vector <- sapply(1:30, function(i) {
        ## Cutting for space and to avoid giving away answers.
        ## a few lines get the complete records in each 
        ## file and count them. 
        if(complete.record.count > threshold) {
          new.correlation <- cor(complete.record$val1, complete.record$val2)
          }  else {
            new.correlation <- NA   
          }
        new.correlation #return value of anonymous function
      })
      # more null value handling#
      example.vector #return value of example.function
    }
    

    However, it is unclear how the index i factors into the anonymous function and the question is not reproducible …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two related questions. First, i have a some information such as: (732,
I am just starting to learn design patterns and I have two questions related
I have two related questions one general and one specific to the project I'm
I have two questions related to the performance of Dozer using its Java API:
I have two cmake-related problems: first, I can't make it to find the includes
I have two questions related to the JavaScript's setInterval() method. I haven't found any
I have two questions related to function objects and function pointers, Question : 1
I actually have two related questions: Can predefined constants be expanded in the task
I have two closely related questions: First, how can the Haskell's Arrow class be
I have two font related questions on Windows 8 metro style app development: is

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.