Question



Question sapply about

Hi Dr. Ballings.

In order assign dummy variables to the dataset, I am using the following code:

for (i in (2:length(sorted[,1]))){
sorted[i,8]<-(ifelse ((sorted[i,6]/sorted[i,3])>=1.01,1,0))}

It works, but it takes a long time to do so (about 15 minutes)

Could you help me out as to how should I proceed in applying the ifelse function with one of the 'apply' methods. I was thinking sapply, but
I am not sure how.

So far I get stuck here:
dataset->combined excel files you provided
unlist(lapply(dataset[,6], function(x) ifelse....))












Answers and follow-up questions





Answer or follow-up question 1

Dear student,

There are two ways to approach the problem (I assume you are working on the large assignment
where you want to model the increase in tomorrow's stock price by 1%).

First do an ifelse. For example:
dependent <- ifelse(sorted$close/sorted$open >= 1.01,1,0)

Then shift dependent up by one value. We did an exercise on this in class.
Find out more about it by searching for the word lag in the book.

Michel Ballings







Sign in to be able to add an answer or mark this question as resolved.