Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 211995

Error when mutating a dataframe in R to add a column with an if condition

$
0
0

I am trying to mutate the following dataframe to add a column at the end which categories each row with "Period_1""Period_2" or "Other" depending on the date.

I've reproduced my code below

# create the start and end dates of the two periods
SampleOneStartDate <- ymd(20020101)
SampleOneEndDate <- ymd(20080101)
SampleTwoStartDate <- ymd(20100101)
SampleTwoEndDate <- ymd(20181130)

# creates the sample of countries and places them in groups (BRICS, etc)
cur.sample <-
  read_rds(paste0(getwd(), "/settings/Equities.rds")) %>% 
  mutate(Ticker = paste0(Ticker, " Index"))

cur.sample.tickers <- cur.sample %>% pull(Ticker)

data.cur <-
  read_rds(paste0(getwd(), "/Data/Cncy.rds")) %>% 
  mutate(Period = ifelse(date >= SampleOneStartDate & date <= SampleOneEndDate, "Period_1",
                         ifelse(date >= SampleTwoStartDate & date <= SampleTwoEndDate, "Period_2", "Other"))) %>% 
  filter(Period %in% c("Period_1", "Period_2")) %>% filter(Ticker %in% cur.sample.tickers) 



The following error appears when I run the second part of the code chunk

Warning messages:
1: In ifelse(date >= SampleOneStartDate & date <= SampleOneEndDate,  :
  Incompatible methods ("Ops.factor", ">=.Date") for ">="
2: In ifelse(date >= SampleOneStartDate & date <= SampleOneEndDate,  :
  Incompatible methods ("Ops.factor", "<=.Date") for "<="
3: In ifelse(date >= SampleTwoStartDate & date <= SampleTwoEndDate,  :
  Incompatible methods ("Ops.factor", ">=.Date") for ">="
4: In ifelse(date >= SampleTwoStartDate & date <= SampleTwoEndDate,  :
  Incompatible methods ("Ops.factor", "<=.Date") for "<="

I've reproduced the dataframe cur.sample below

structure(list(Country = structure(c(2L, 15L, 8L, 4L, 16L, 11L, 
1L, 3L, 5L, 12L, 17L, 18L, 13L, 10L, 9L, 6L, 7L, 14L, 19L), .Label = c("Argentina", 
"Brazil", "Chile", "China", "Colombia", "Czech", "Hungary", "India", 
"Korea", "Malaysia", "Mexico", "Peru", "Philipines", "Poland", 
"Russia", "South Africa", "Taiwan", "Thailand", "Turkey"), class = "factor"), 
    Name = structure(c(1L, 12L, 15L, 18L, 9L, 6L, 11L, 7L, 3L, 
    5L, 19L, 16L, 14L, 4L, 10L, 13L, 2L, 17L, 8L), .Label = c("BOVEPSA", 
    "BUX Index", "COLCAP Index", "FTSE KLCI", "IGBVL Index", 
    "IPC", "IPSA Index", "Istambul", "JSE Top 40", "KOPSI Index", 
    "MERVAL Index", "MOEX", "PSE PX", "PSEi Index", "SENSEX", 
    "SET 50", "SIX", "SSE Composite Index", "TWSE Index"), class = "factor"), 
    Ticker = c("MXBR Index", "MXRU Index", "MXIN Index", "MXCN Index", 
    "MXZA Index", "MXMX Index", "MXAR Index", "MXCL Index", "MXCO Index", 
    "MXPE Index", "TAMSCI Index", "MXTH Index", "MXPH Index", 
    "MXMY Index", "MXKR Index", "MXCZ Index", "MXHU Index", "MXPL Index", 
    "MXTR Index"), Group = structure(c(2L, 2L, 2L, 2L, 2L, 4L, 
    4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L), .Label = c("Asia", 
    "BRICS", "Europe", "Latin America"), class = "factor")), class = "data.frame", row.names = c(NA, 
-19L))

Here is a tiny snippet of my dataset data.str. There are multiple tickers and dates up until 2019 in the actual one

    date          Ticker      Name           Value
1   2000-01-03  MXBR Index  Brazil_Index    874.0292
2   2000-01-04  MXBR Index  Brazil_Index    816.4275
3   2000-01-05  MXBR Index  Brazil_Index    831.9147
4   2000-01-06  MXBR Index  Brazil_Index    827.5026
5   2000-01-07  MXBR Index  Brazil_Index    843.3013
6   2000-01-10  MXBR Index  Brazil_Index    879.8027
7   2000-01-11  MXBR Index  Brazil_Index    856.7554
8   2000-01-12  MXBR Index  Brazil_Index    852.7734
9   2000-01-13  MXBR Index  Brazil_Index    885.2533
10  2000-01-14  MXBR Index  Brazil_Index    907.2839
11  2000-01-17  MXBR Index  Brazil_Index    929.7444
12  2000-01-18  MXBR Index  Brazil_Index    925.2186
13  2000-01-19  MXBR Index  Brazil_Index    902.8753
14  2000-01-20  MXBR Index  Brazil_Index    890.9248
15  2000-01-21  MXBR Index  Brazil_Index    883.0727
16  2000-01-24  MXBR Index  Brazil_Index    889.2529
17  2000-01-25  MXBR Index  Brazil_Index    887.0005
18  2000-01-26  MXBR Index  Brazil_Index    889.8742
19  2000-01-27  MXBR Index  Brazil_Index    886.3568
20  2000-01-28  MXBR Index  Brazil_Index    858.4258
21  2000-01-31  MXBR Index  Brazil_Index    839.7178
22  2000-02-01  MXBR Index  Brazil_Index    842.6707
23  2000-02-02  MXBR Index  Brazil_Index    852.6062
24  2000-02-03  MXBR Index  Brazil_Index    886.2512
25  2000-02-04  MXBR Index  Brazil_Index    911.1760
26  2000-02-07  MXBR Index  Brazil_Index    918.9617
27  2000-02-08  MXBR Index  Brazil_Index    945.2223
28  2000-02-09  MXBR Index  Brazil_Index    926.6250
29  2000-02-10  MXBR Index  Brazil_Index    925.2223
30  2000-02-11  MXBR Index  Brazil_Index    904.5837
31  2000-02-14  MXBR Index  Brazil_Index    879.1468
32  2000-02-15  MXBR Index  Brazil_Index    885.6335
33  2000-02-16  MXBR Index  Brazil_Index    896.0780
34  2000-02-17  MXBR Index  Brazil_Index    894.2178
35  2000-02-18  MXBR Index  Brazil_Index    876.0983
36  2000-02-21  MXBR Index  Brazil_Index    880.9504
37  2000-02-22  MXBR Index  Brazil_Index    866.5338


Can anyone point me in the right direction as to how I should debug the ifelse command?

TIA


Viewing all articles
Browse latest Browse all 211995

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>