Advertisement
60E1CluBSteP127

Kansantalouspeli data frame generator

Feb 26th, 2022
806
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. read.round.lines <- function(path, n, type) {
  2.   con <- file(paste(path,paste(n,type,sep="."),sep="/"),"r") # make the path
  3.   lin <- readLines(con) # read line by line
  4.   close(con)
  5.   lin[lin != ""] # remove empty lines
  6. }
  7.  
  8. read.round.df <- function(path, n, type) {
  9.   lin <- read.round.lines(path, n, type) # read file line by line
  10.   func <- function(str) strsplit(str, " ") # anonymous func for splitting
  11.   getname <- function(list) list[[1]][1] # get name of var
  12.   indnum <- if(type == "inp" && n != 0) 3 else 2 # workaround: inputs 1,2,... have odd data.
  13.   getnum <- function(list) list[[1]][indnum] # get value of var
  14.   linsplit <- lapply(lin,func) # get all split lines
  15.   names <- lapply(linsplit,getname) # use linsplit to get names
  16.   nums <- lapply(linsplit,getnum) # use linsplit to get numbers
  17.   df <- data.frame(nums, row.names = n) # make a data frame
  18.   names(df) <- names # set names
  19.   df
  20. }
  21.  
  22. read.rounds.df <- function(path, ns, type) {
  23.   df <- data.frame() # init an empty data rame
  24.   for(i in ns) { # go thru all rounds
  25.     dfi <- read.round.df(path, i, type) # load specific round
  26.     df <- rbind(df,dfi) # add new observation
  27.   }
  28.   df # and return
  29. }
Advertisement
RAW Paste Data Copied
Advertisement