-1

This is probably a stupid question, but I can't find an answer to it.

My data frame looks like this:

           FISV.Volume FISV.Adjusted FISV.Volume.1
2007-01-03     8545600       12.8925       7635600
2007-01-04     7635600       13.1650       4457200
2007-01-05     4457200       13.1150       3542000
2007-01-08     3542000       13.1475       3908800
2007-01-09     3908800       13.1875       3450800
2007-01-10     3450800       13.1525       3166800

And my code looks like this:

outcomeSymbol <- "FISV.Volume" 
 
nasdaq100["outcome"] <- ifelse(nasdaq100[,paste0(outcomeSymbol,".1")] > nasdaq100[outcomeSymbol],1,0)

           FISV.Volume FISV.Adjusted FISV.Volume.1 FISV.Volume
2007-01-03     8545600       12.8925       7635600           0
2007-01-04     7635600       13.1650       4457200           0
2007-01-05     4457200       13.1150       3542000           0
2007-01-08     3542000       13.1475       3908800           1
2007-01-09     3908800       13.1875       3450800           0
2007-01-10     3450800       13.1525       3166800           0

Now the last column should be called "outcome" and not "FISV.Volume" (I get the same result when I use $ when specifying the name of the new column)

Full code here: http://amunategui.github.io/wallstreet/

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
L.H.
  • 1
  • 2
  • Is it a data.frame or `matrix – akrun Mar 30 '21 at 17:45
  • But the code line creating the new column is `nasdaq100$outcome <- ifelse(etc)`. – Rui Barradas Mar 30 '21 at 18:00
  • This is the code in which the dataframe was specified: `nasdaq100 <- as.data.frame(merge(nasdaq100,lm1=stats::lag(nasdaq100[,outcomeSymbol],-1)))` – L.H. Mar 30 '21 at 18:04
  • As already stated above using `nasdaq100$outcome` returns the same result as `nasdaq100["outcome"]`. (`nasdaq100[["outcome"]]`also returns that same result) – L.H. Mar 30 '21 at 18:09
  • @RuiBarradas can you please make my question public again? Because this question is not similar to the question you stated it is similar to. I know how to resolve my question now. Using `nasdaq100["outcome"] <- as.data.frame <- c(ifelse(nasdaq100[,paste0(outcomeSymbol,".1")] > nasdaq100[outcomeSymbol],1,0))` worked. It was not a problem of wrong parentheses as you stated. However I still don't fully get why by using my original code the outcome column got the same name as one of the columns of the argument. – L.H. Mar 31 '21 at 15:13
  • Done but can you post sample data in `dput` format? Please edit **the question** with the output of `dput(nasdaq100)`. Also, you are missing a comma in `nasdaq100[outcomeSymbol]` just before the column name variable. – Rui Barradas Mar 31 '21 at 16:16

1 Answers1

0

It seems that ifelse is getting confused by the paste0 command. First let's use dput to provide the data in a reproducible format:

nasdaq100 <- structure(list(FISV.Volume = c(8545600L, 7635600L, 4457200L, 
3542000L, 3908800L, 3450800L), FISV.Adjusted = c(12.8925, 13.165, 
13.115, 13.1475, 13.1875, 13.1525), FISV.Volume.1 = c(7635600L, 
4457200L, 3542000L, 3908800L, 3450800L, 3166800L)), class = "data.frame", row.names = c("2007-01-03", 
"2007-01-04", "2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10"
))

Not using paste0 works fine:

nasdaq100[, "outcome"] <- ifelse(nasdaq100[, "FISV.Volume.1"] > nasdaq100[, outcomeSymbol],1,0)
str(nasdaq100)
# 'data.frame': 6 obs. of  4 variables:
#  $ FISV.Volume  : int  8545600 7635600 4457200 3542000 3908800 3450800
#  $ FISV.Adjusted: num  12.9 13.2 13.1 13.1 13.2 ...
#  $ FISV.Volume.1: int  7635600 4457200 3542000 3908800 3450800 3166800
#  $ outcome      : num  0 0 0 1 0 0

This seems to work with your code by stripping the name attribute:

nasdaq100["outcome"] <- unname(ifelse(nasdaq100[, paste0(outcomeSymbol,".1")] > nasdaq100[outcomeSymbol], 1, 0))
nasdaq100
#            FISV.Volume FISV.Adjusted FISV.Volume.1 outcome
# 2007-01-03     8545600       12.8925       7635600       0
# 2007-01-04     7635600       13.1650       4457200       0
# 2007-01-05     4457200       13.1150       3542000       0
# 2007-01-08     3542000       13.1475       3908800       1
# 2007-01-09     3908800       13.1875       3450800       0
# 2007-01-10     3450800       13.1525       3166800       0
dcarlson
  • 10,936
  • 2
  • 15
  • 18