ruby on rails - how to pass unknown number of arguments to a function in R programming -
i parsing csv multiple columns. number of columns not fixed in csv file. varies 5 10. need recreate data.frame these columns inside function. wondering if there multiple arguments functionality in r 1 in ruby(*args). if not, how achieve this??? searched bit , found if have col name as
col1 col2
i can use:
list <- ls(pat="^col\\d$")
and pass list argument function, pass column names, characters, not values these column names carrying.
any suggestions????
edit: parsing file ror app , using rinruby gem call r functions. parsing csv ruby , passing individual column contents single variable in r. in r, need create data.frame. not data frame originally. in method cal_norm
below assigning variables in r using loop names col1, col2, col3....and on.
here rails code:
class uploadscontroller < applicationcontroller attr_accessor :calib_data, :calib_data_transpose, :inten_data, :pr_list def index @uploads = upload.all @upload = upload.new respond_to |format| format.html format.json { render json: @uploads } end end def create @upload = upload.new(params[:upload]) directory = "public/" io_calib = params[:upload][:calib] io_inten = params[:upload][:inten] name_calib = io_calib.original_filename name_inten = io_inten.original_filename calib_path = file.join(directory, "calibs", name_calib) inten_path = file.join(directory, "intens", name_inten) respond_to |format| if @upload.save @calib_data, @calib_data_transpose = import(calib_path) @inten_data = import_ori(inten_path) #probe list of uploaded file @probe_list = calib_data_transpose[0] logger.debug @probe_list.to_s flash[:notice] = "files uploaded!!" format.html #format.js #{ render json: @upload, status: :created, location: @upload } else flash[:notice] = "error in uploading!!" format.html { render action: "index" } format.json { render json: @upload.errors, status: :unprocessable_entity } end end end def cal_norm #ajax request data = params['data'].split(',') in 0..@calib_data_transpose.length - 1 r.assign "col#{i}", @calib_data_transpose[i] end r.assign "cells", @inten_data r.assign "pr", data r.eval <<-eof # make sure convert them in character , numeric vectors #match selected pr in table #convert found row of values data.frame numeric #divide each column of table respective pr values , create new table repat different pr. #make new table ce count , different probe normalization , calculate individual pr #finally return data.frame pr names , cell counts #return individual columns array not in form of matrix/data.frame eof end def import(file_path) array = import_ori(file_path) array_splitted = array.map {|a| a.split(",")} array_transpose = array_splitted.transpose return array_splitted, array_transpose end def import_ori(file_path) string = io.read(file_path) array = string.split("\n") array.shift return array end end
post updated question:
i utter newbie of ruby found example here: col wise data
here column wise data read col_data, 0 here (col) index (no ruby testing :( )
require 'csv' col_data = [] csv.foreach(filename) {|row| col_data << row[0]}
assign col data variables col1...coln, , create counter number of columns (syntax might not 100% correct)
for in 0..@calib_data_transpose.length - 1 #r.assign "col#{i}", @calib_data_transpose[i] csv.foreach(filename) {|row| "col#{i}" << row[i]} end r.col_count=@calib_data_transpose.length - 1
and once col1..coln created, combine column data 1 index @ time starting @ = 1. result data.frame order of columns col1.... coln.
r.eval <<-eof for(i in 1:col_count) { if (i==1) { df<-data.frame(get(paste0("col",i))) } else { df<-cbind(df,get(paste0("col",i))) } names(df)[i]<-paste0("col",i) } eof
let know if helps...
not relevant updated question anymore retaining posterity.
subset data.frame given pattern
as roland stated above read.csv
read entire file, since wish control columns retained in data.frame following:
using data(mtcars)
sample data.frame
code:
read in data:
> data(mtcars) > head(mtcars) mpg cyl disp hp drat wt qsec vs gear carb mazda rx4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 mazda rx4 wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 hornet 4 drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 hornet sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
subset data condition, columns beginning alphabet 'c'
> head(mtcars[,grep("^c",colnames(mtcars))]) cyl carb mazda rx4 6 4 mazda rx4 wag 6 4 datsun 710 4 1 hornet 4 drive 6 1 hornet sportabout 8 2 valiant 6 1
here '^c'
similar pattern pat="^col\\d$"
question. substitute '^c'
regular expression of choice e.g '^col'
.the '^c'
match pattern beginning alphabet 'c', match @ end of string use '$c'
Comments
Post a Comment