2014-04-14 58 views
0

我正在解析具有多列的csv。列數在csv文件中不固定。它從5到10不等。我需要重新創建一個函數內的這些列的data.frame。我想知道在R中是否有任何多個參數功能,如Ruby(* args)中的一個。 如果沒有,如何做到這一點??? 我搜索了一下,發現,如果我有一個山坳名如何將未知數量的參數傳遞給R編程中的函數

col1 
col2 

我可以使用:

list <- ls(pat="^col\\d$") 

,並通過這個列表作爲參數傳遞給一個函數,但它會通過單純的列名稱作爲字符,而不是這些列名所攜帶的值。

任何建議????

編輯: 我從RoR應用程序解析文件並使用RinRuby gem來調用R函數。所以從ruby解析csv並將單個列內容作爲R中的單個變量傳遞。現在在R中,我需要創建一個data.frame。所以實際上它本來不是一個數據框。因此,在下面的cal_norm方法中,我使用名爲col1,col2,col3 ....等等的循環來分配R中的變量。

這裏是Rails代碼:

class UploadsController < ApplicationController 

    attr_accessor :calib_data, :calib_data_transpose, :inten_data, :pr_list 

    def index 
    @uploads = Upload.all 

    @upload = Upload.new 

    respond_to do |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 do |format| 
    if @upload.save 
    @calib_data, @calib_data_transpose = import(calib_path) 
    @inten_data = import_ori(inten_path) 
    #probe list of the uploaded file 
    @probe_list = calib_data_transpose[0] 
    logger.debug @probe_list.to_s 
    flash[:notice] = "Files were successfully 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(',') 

    for i in [email protected]_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 to convert them in character and numeric vectors 

#match the selected pr in the table 

#convert the found row of values from data.frame to numeric 

#divide each column of the table by the respective pr values and create a new table repat it with different pr. 

#make a new table with the ce count and different probe normalization and calculate for individual pr 

#finally return a data.frame with pr names and cell counts 

#return individual columns as an array not in the 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 
+1

我不明白這個問題。 'read.csv'返回一個data.frame。 – Roland

+1

我也沒有。你有一個CSV,每行有5到10個項目?你打算如何將它放在矩形數據框中?您可以使用'fill'參數指向'read.csv',用NA標記填充它。否則...什麼? – Spacedman

+0

只需使用'read.csv'或'read.table'閱讀您的csv。這兩個函數都不關心你的csv有多少列。也許你可以將你的問題編輯得更加明確一些,比如「用這些列回顧data.frame」。 –

回答

2

公佈更新後的問題:

我是紅寶石的絕對新手,但在這裏發現了這個例子:col wise data

這裏逐列數據被讀取進入col_data,這裏的0是(col)索引(沒有Ruby用於測試:()

require 'csv' 
col_data = [] 
CSV.foreach(filename) {|row| col_data << row[0]} 

山坳數據分配給一個變量COL1 ...科隆,和列數創建一個計數器(語法可能不是100%正確的)

for i in [email protected]_data_transpose.length - 1 
#R.assign "col#{i}", @calib_data_transpose[i] 
CSV.foreach(filename) {|row| "col#{i}" << row[i]} 
end 

[email protected]_data_transpose.length - 1 

一旦col1..coln創建,結合列數據從i = 1開始,每次只有一個索引。結果是一個data.frame,列的順序爲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 

讓我們知道,如果這有助於...


不以更新的問題了很相關,但保留它爲後人。

對於給定的圖案

作爲羅蘭子集data.frame上述read.csv說明將讀取整個文件,因爲要控制的列被保留在數據。框,你可以做到以下幾點:

使用data(mtcars)爲樣本data.frame

代碼:

讀入的數據:

> data(mtcars) 
> head(mtcars) 
        mpg cyl disp hp drat wt qsec vs am 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 

子集爲某些條件的數據,說列開始用字母'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 

這裏'^c'類似於您的問題的模式pat="^col\\d$"。你可以用'^c'來代替你的選擇,例如'^col''^c'將匹配以字母'c'開頭的任何模式,以匹配字符串的末尾使用'$c'

+0

Vivek,我編輯了我的問題。抱歉,我錯過了這個重要部分 – JstRoRR

相關問題