Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. transakcje<-read.table(file = "transakcje.csv", nrows = 1000, sep=",", header= TRUE)
  2. konta<-read.table(file = "kona.csv", nrows=100, sep=",", header = TRUE)
  3.  
  4. #liczymy srednia, dane wczytujemy do pamieci porcjami
  5. readToChunk<-function(fileName, colname, chunkSize){
  6. file<-fileName
  7. connection<-file(description = file,open="r")
  8. index<-0
  9. counter<-0
  10. suma<-0
  11. chunk<-read.table(connection,nrows=chunkSize,header=TRUE,sep=",")
  12. columNames <- names(chunk)
  13.  
  14. repeat{
  15. index<-index+1
  16. print(paste0("sum: ", suma,"counter: ",counter))
  17. #chunk<-na.omit(chunk)
  18. suma<-suma+sum(chunk[[colname]])
  19. counter<-counter+nrow(chunk)
  20. if (nrow(chunk) !=chunkSize){
  21. print(paste0("suma:", suma, "counter:", counter))
  22. break;
  23. }
  24.  
  25. chunk<-read.table(connection, nrows=chunkSize, fill=TRUE,skip = 0, header=FALSE, sep=",",col.names = columNames)
  26. }
  27. close.connection(connection)
  28. suma/counter
  29. }
  30.  
  31. #readtoChunk("transacje.csv", "amount", 1000) w konsoli
  32.  
  33.  
  34. #ZADANIE2 DO BAZY
  35. #wczytywanie do bazy
  36.  
  37. install.packages("DBI")
  38. install.packages("RSQLite")
  39. library(DBI)
  40. library(RSQLite) #ladowanie
  41.  
  42. readtoDataBase<-function(filepath,basepath,tableName, chunkSize,sepa=","){
  43. conwithFile<-file(description = filepath,open="r")
  44. dbCon <-dbConnect(SQLite(), basepath)
  45. dataChunk<-read.table(conwithFile,nrows = chunkSize, header = TRUE, fill = TRUE, sep=sepa)
  46. columnNames<-names(dataChunk)
  47. dbwriteTable(dbCon,tableName,dataChunk,append=TRUE,overwrite = FALSE)
  48.  
  49. repeat{
  50. if (nrown(dataChunk)!=chunkSize)
  51. break
  52. dataChunk<-read.table(conwithFile,nrows=chunkSize, skip=0, header-FALSE, fill=TRUE, sep=sepa, col.names = columnNames)
  53. dbwriteTable(dbCon,tablename,dataChunk,append=TRUE, overwrite=FALSE)
  54. }
  55. close.connectio(conwithFile)
  56. dbDisconnect(dbCon)
  57. }
  58.  
  59. #readtoDataBase("kona.csv", "baza.sqllite", "konta",100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement