import_sql.r 869 B

12345678910111213141516171819202122232425
  1. library(foreach)
  2. indata<-read.table("/home/ademant/src/rasolar/data/data_433058.txt",fill=TRUE,stringsAsFactors=FALSE)
  3. raw_dat=unlist(sapply(1:nrow(indata),function(i){return(strsplit(indata[i,],";")[[1]])}))
  4. var_names=unique(sapply(1:length(raw_dat),function(i){return(head(strsplit(raw_dat[i],":")[[1]],1))}))
  5. raw_row=as.data.frame(t(as.data.frame(rep(NA,length(var_names)))))
  6. names(raw_row)<-var_names
  7. intable<-foreach(i=1:nrow(indata),.combine=rbind)%do%{
  8. tid<-strsplit(indata[i,],";")[[1]]
  9. tout<-as.data.frame(t(sapply(tid,function(i){return(strsplit(i,":")[[1]])})))
  10. tout[,2]<-as.numeric(as.character(tout[,2]))
  11. tout[,1]<-as.character(tout[,1])
  12. names(tout)<-c("var","val")
  13. tout$time<-NA
  14. if("time"%in%tout$var){
  15. tout$time<-head(tout[tout$var=="time","val"],1)
  16. tout<-tout[tout$var!="time",]
  17. }
  18. return(tout)
  19. }
  20. solar_table<-rbind(solar_table,intable)