在比较两个向量中的两个浮点元素时遇到相同和all.equal的问题

时间:2013-03-18 20:11:27

标签: r floating-point comparison floating-accuracy

这是我发布的yesterday问题的后续跟进。我似乎无法在R中对浮点比较做正确的事。昨天我使用>=来比较两个浮点值,但这个dint似乎得到了正确的结果。

今天,我tried在两个向量上以元素方式运行all.equal,这两个向量产生mean difference,这对此应用程序不起作用。我需要比较函数来返回一个向量。然后,我找到identical并将其与mapply合并。这变得更准确,但不是100%准确。我究竟做错了什么?由于这是财务数据,我应该使用decimal data type吗?如果是这样,怎么样?

从昨天的帖子开始(使用更新的代码,反映当前的挫败感):

目标是:将数据读入data.frame,平均取得昨天的HighLowClose价格;并且,将今天的开盘价与昨天的平均价进行比较。

在大数据上运行脚本后,我发现R 中的结果与Excel中运行的类似分析不匹配。我把问题缩小到了它的基本部分。我的测试文件test.csv看起来像这样,包括最后一行末尾的新行:

<TICKER>,<DATE>,<TIME>,<OPEN>,<LOW>,<HIGH>,<CLOSE>
EURUSD,20020311,0:00:00,0.8733,0.873,0.877,0.8749
EURUSD,20020312,0:00:00,0.8749,0.8704,0.876,0.8754
EURUSD,20020313,0:00:00,0.8753,0.8725,0.878,0.8754
EURUSD,20020314,0:00:00,0.8753,0.8752,0.8841,0.8823
EURUSD,20020315,0:00:00,0.8823,0.8808,0.8868,0.8823
EURUSD,20020318,0:00:00,0.8809,0.878,0.8828,0.8821
EURUSD,20020319,0:00:00,0.8821,0.8796,0.884,0.8816
EURUSD,20020320,0:00:00,0.8815,0.8786,0.8857,0.8855
EURUSD,20020321,0:00:00,0.8854,0.8806,0.8857,0.8823

我的代码:

# Read in test file
raw <- read.csv('test.csv', header=TRUE, sep=",")

# Convert date and dump dat into data frame
stripday <- strptime(raw$X.DATE, format="%Y%m%d")
data <- data.frame(stripday, raw)

# Drop unused data columns and name used columns
drops <- c("X.DATE.", "X.TIME.", "X.TICKER.")
data <- data[, !(names(data) %in% drops)]
colnames(data) <- c("Date", "Open", "Low", "High", "Close")

# Convert values from facotrs to numeric
data[,2] <- as.numeric(as.character(data[,2]))
data[,3] <- as.numeric(as.character(data[,3]))
data[,4] <- as.numeric(as.character(data[,4]))
data[,5] <- as.numeric(as.character(data[,5]))

# Take average of High, Low, and Close 
data[['Avg']] <- NA
data[['Avg']][2:9] <- (
    data[['High']][1:8] + 
    data[['Low']][1:8] + 
    data[['Close']][1:8]) / 3

# Is Open greater than or equal to Average
data[['OpenGreaterThanOrEqualAvg']] <- NA
data[['OpenGreaterThanOrEqualAvg']][2:9] <- 1 * (mapply(identical,data[['Open']][2:9], data[['Avg']][2:9]) | data[['Open']][2:9] > data[['Avg']][2:9])

# Write data to .csv
write.table(data, 'output.csv', quote=FALSE, sep=",", row.names=FALSE)

请注意,3/14/2002应该有1而不是0。

0 个答案:

没有答案