R中的数据类型转换

时间:2017-08-18 15:11:33

标签: r

变量行在main.R文件中定义为tProcRows <<- 0,默认情况下为'double' 而不是我在另一个R文件中的另一个函数 -

tProcRows <<- as.double(row.names(rawData)[nrow(rawData)]) + tProcRows 

令人惊讶的是 -

> tProcRows 
numeric(0)

我正在从rawData文件中阅读csv,并在阅读后通过2-3个过滤器。所以我想以这种方式计算我处理过的行数,以便下次rawData读取时可以跳过那么多行。而且我似乎无法做到这一点......

这是 - main.R

rm(list=ls())
cat("\014")  

# ffName    <<- "BTPdata004.csv"  # data file full Name
ffName    <<- "BUND009.csv"  # data file full Name
tProcRows <<- 0        # total processed rows so far form the file 
cProcRows <<- 0             # total processed rows form currently loaded chunk ]
chSize    <<- 100000            # Chunk size
lastFlag  <<- 0              # flag for indicating last chunk from the file 
opData    <<- data.frame()
TimeFrame <<- 5
fileName  <<- "R_OHCL_lite_DD.csv" # Output file

maxLen <<- 0
minLen <<- 1000000

if (file.exists(fileName)) file.remove(fileName) # delete old one

StartTimeG <<- Sys.time()


Open   <<- vector(mode = "numeric",length = 0)
Close  <<- vector(mode = "numeric",length = 0)
High   <<- vector(mode = "numeric",length = 0)
Low    <<- vector(mode = "numeric",length = 0)
Volume <<- vector(mode = "numeric",length = 0)
Time   <<-   vector(mode = "character",length = 0)
Date   <<-   vector(mode = "character",length = 0)

source("loadData.R")
source("processData.R")
source("saveData.R")




############################### Repeat utill complete data is processed ###########################

# while(lastFlag != 1) {
# load data
 #print("#####   main: Let's Load some data")
# loadData()

# process data
  print("#####   main: Let's process the data")
processData()

# append the processed data frame to Storage file

# }

if(length(Open) < 100){
  opData <<- cbind(Date,Time,Open,High,Low,Close,Volume)
  saveData()  # saveToFile
}



print("#####   End From Main Function, Total time taken-->")
time.taken = Sys.time() - StartTimeG
print(time.taken)

############################### Repeat utill complete data is processed ###########################

loadData.R

# loads the file in chunks

library("iotools")
library("chron")
library("lubridate")


loadData <- function(){



   # if(tProcRows != 0)  tProcRows <<- as.numeric(row.names(rawData)[nrow(rawData)],length=1) + tProcRows


      cProcRows <<- 0 
      nskip     <<- tProcRows
     # rawData   <<- NULL




      rawData <<- read.csv.raw(file = ffName,sep=",",skip=nskip, nrows = chSize,nrowsClasses = 5000)   



      if(nrow(rawData) < chSize){
        lastFlag <<- 1  # this chunk is the last from the file 
      }



      rawData <<- subset.data.frame(rawData,rawData$Type=="Trade")
      rawData$Date <<- as.Date(rawData$'Date[G]',format = "%d-%b-%Y")
      rawData$Time <<- lubridate::hms(rawData$"Time[G]")

      if(lastFlag!=1){
      lastDay <<- rawData$Date[nrow(rawData)]  # last complete day
      rawData <<- subset.data.frame(rawData,rawData$Date < lastDay)
      }

      ############################## this is the line #########

       tProcRows <<- tProcRows + as.numeric(row.names(rawData)[nrow(rawData)]) 
       print(tProcRows)

      ###########################################################

      rawData$`#RIC`        <<-   NULL
      rawData$Type          <<-   NULL
      rawData$`GMT Offset`  <<-   NULL
      rawData$`Bid Price`   <<-   NULL
      rawData$`Bid Size`    <<-   NULL
      rawData$`Ask Price`   <<-   NULL
      rawData$`Ask Size`    <<-   NULL
      rawData$Qualifiers    <<-   NULL
      rawData$'Date[G]'     <<-   NULL



}#function

输出

[1] "#####   main: Let's process the data"
[1] 88230
[1] "##### File Saved-------> "
Time difference of 2.2081 secs
numeric(0)
numeric(0)
[1] "##### File Saved-------> "
Time difference of 5.0582 secs
numeric(0)
[1] "##### File Saved-------> "
Time difference of 7.1483 secs
numeric(0)
[1] "##### File Saved-------> "
Time difference of 9.4814 secs
numeric(0)
[1] "##### File Saved-------> "
Time difference of 11.5785 secs

所以它第一次工作,但在那次迭代之后什么都没有......

PS。还有另一个文件,但那些与...这个变量

无关

1 个答案:

答案 0 :(得分:0)

似乎你的代码需要清理一下:

  if(lastFlag!=1){
  lastDay <<- rawData$Date[nrow(rawData)]  # last complete day
  rawData <<- subset.data.frame(rawData,rawData$Date < lastDay)
  }

我认为&#34; rawData&#34;这里可能是一个空的数据帧但不能被检查;

我们假设:

rawData <- data.frame(x=c(), y=c())
tProcRows <- 100

所以:

tProcRows <- tProcRows + as.numeric(row.names(rawData)[nrow(rawData)]) 
print(tProcRows)  

输出:

numeric(0)