我正在尝试更改以下数据帧,以便在末尾添加一列,该列根据日期将每行分类为"Period_1“、"Period_2”或"Other“。
我已经复制了下面的代码
# 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)
当我运行代码块的第二部分时,出现以下错误
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 "<="
我已经复制了下面的数据帧cur.sample
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))
这是我的数据集data.str
的一小段代码。实际的报价器中有多个报价器和截止到2019年的日期
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
关于如何调试ifelse命令,有人能给我指出正确的方向吗?
提亚
发布于 2020-01-30 20:13:24
我们只需要将'date‘更改为Date
类,它应该可以工作
data.cur$date <- as.Date(dta.cur$date)
因为错误主要是因为在需要Date
类的情况下处理factor
列比较
https://stackoverflow.com/questions/59993159
复制相似问题