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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:48:45+00:00 2026-06-10T05:48:45+00:00

I have let say a xts object (data) with following values… SPY.Adjusted SMA 2012-08-02

  • 0

I have let say a xts object (data) with following values…

           SPY.Adjusted     SMA
2012-08-02       136.64 137.115
2012-08-03       139.35 137.995
2012-08-06       139.62 139.485
2012-08-07       140.32 139.970
2012-08-08       140.49 140.405
2012-08-09       140.61 140.550
2012-08-10       140.84 140.725

I’m trying to use apply function to append to it the signals if some conditions are met… in this case when the close > SMA. My function:

signal<-function(x,y,z)
  {
    z$signals<-ifelse(x>y,1,0)
  }

and I try to…

apply(data,1,FUN=signal(data$SPY.Adjusted,data$SMA,data))

with returned error:

Error in match.fun(FUN) : 'signal(data$SPY.Adjusted, data$SMA, data)'
  is not a function, character or symbol

What is possibly going wrong? I passed in to it a function which reaches in to the data objected passed into it to create a new column if certain condition is met.

  • 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-10T05:48:47+00:00Added an answer on June 10, 2026 at 5:48 am

    When you call apply with MARGIN=1, it’s like passing each row to FUN. Your function is already vectorized, so you don’t need to use apply. However, your function does not return anything. Try this:

    library(quantmod)
    getSymbols("SPY", src='yahoo', from='2010-01-01', to='2012-01-01')
    dat <- cbind(Ad(SPY), SMA=SMA(Ad(SPY)))
    signal<-function(x,y,z)
    {
         z$signals<-ifelse(x>y,1,0)
         z
    }
    
    tail(signal(dat[, 1], dat[, 2], dat))
    #           SPY.Adjusted     SMA signals
    #2011-12-22       124.08 121.693       1
    #2011-12-23       125.19 121.805       1
    #2011-12-27       125.29 122.108       1
    #2011-12-28       123.64 122.361       1
    #2011-12-29       124.92 122.871       1
    #2011-12-30       124.31 123.276       1
    

    Actually, I try to avoid ifelse in situations like these because it is slower than doing this

    signal<-function(x,y,z)
    {
      z$signals <- 0
      z$signals[x > y] <- 1
      z
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have the following data in R. training = factor(c(1,1,3,2,1,3,2,34,67,34)) test =
Let say I have the following data frame in R: df1 <- data.frame(Item_Name =
Let say I have an array like: Array ( [0] => Array ( [Data]
Let say I have a BitmapData with different pixels representing an object, and some
What I have: Let's say I have a hash like this, with various values
Let say I have the following desire, to simplify the IConvertible's to allow me
For instance, let's say you have ~10 years of daily 1 min data for
Let say I have a class like the following: class MyClass { public function
Let say I have abstract class called: Tenant and Customer. The tenant in this
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment

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.