Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- transakcje<-read.table(file = "transakcje.csv", nrows = 1000, sep=",", header= TRUE)
- konta<-read.table(file = "kona.csv", nrows=100, sep=",", header = TRUE)
- #liczymy srednia, dane wczytujemy do pamieci porcjami
- readToChunk<-function(fileName, colname, chunkSize){
- file<-fileName
- connection<-file(description = file,open="r")
- index<-0
- counter<-0
- suma<-0
- chunk<-read.table(connection,nrows=chunkSize,header=TRUE,sep=",")
- columNames <- names(chunk)
- repeat{
- index<-index+1
- print(paste0("sum: ", suma,"counter: ",counter))
- #chunk<-na.omit(chunk)
- suma<-suma+sum(chunk[[colname]])
- counter<-counter+nrow(chunk)
- if (nrow(chunk) !=chunkSize){
- print(paste0("suma:", suma, "counter:", counter))
- break;
- }
- chunk<-read.table(connection, nrows=chunkSize, fill=TRUE,skip = 0, header=FALSE, sep=",",col.names = columNames)
- }
- close.connection(connection)
- suma/counter
- }
- #readtoChunk("transacje.csv", "amount", 1000) w konsoli
- #ZADANIE2 DO BAZY
- #wczytywanie do bazy
- install.packages("DBI")
- install.packages("RSQLite")
- library(DBI)
- library(RSQLite) #ladowanie
- readtoDataBase<-function(filepath,basepath,tableName, chunkSize,sepa=","){
- conwithFile<-file(description = filepath,open="r")
- dbCon <-dbConnect(SQLite(), basepath)
- dataChunk<-read.table(conwithFile,nrows = chunkSize, header = TRUE, fill = TRUE, sep=sepa)
- columnNames<-names(dataChunk)
- dbwriteTable(dbCon,tableName,dataChunk,append=TRUE,overwrite = FALSE)
- repeat{
- if (nrown(dataChunk)!=chunkSize)
- break
- dataChunk<-read.table(conwithFile,nrows=chunkSize, skip=0, header-FALSE, fill=TRUE, sep=sepa, col.names = columnNames)
- dbwriteTable(dbCon,tablename,dataChunk,append=TRUE, overwrite=FALSE)
- }
- close.connectio(conwithFile)
- dbDisconnect(dbCon)
- }
- #readtoDataBase("kona.csv", "baza.sqllite", "konta",100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement