我正在解析具有多列的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
我不明白這個問題。 'read.csv'返回一個data.frame。 – Roland
我也沒有。你有一個CSV,每行有5到10個項目?你打算如何將它放在矩形數據框中?您可以使用'fill'參數指向'read.csv',用NA標記填充它。否則...什麼? – Spacedman
只需使用'read.csv'或'read.table'閱讀您的csv。這兩個函數都不關心你的csv有多少列。也許你可以將你的問題編輯得更加明確一些,比如「用這些列回顧data.frame」。 –