【发布时间】:2014-04-14 13:04:17
【问题描述】:
我正在解析一个包含多列的 csv。 csv 文件中的列数不固定。它从 5 到 10 不等。我需要在函数内重新创建包含这些列的 data.frame。我想知道 R 中是否有像 Ruby(*args) 中的多参数功能。 如果没有,如何实现这个??? 我搜索了一下,发现如果我有一个 col 名称作为
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 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 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。 -
我也没有。您有一个 CSV,每行包含 5 到 10 个项目?您将如何将其放入矩形数据框中?您可以使用
fill参数到read.csv用NA 标记填充它。否则……什么? -
只需使用
read.csv或read.table读取您的csv。这两个函数都不关心你的 csv 有多少列。也许您可以将您的问题编辑得更明确一点,就像您所说的“使用这些列重新创建一个 data.frame”的确切含义。 -
对不起,我没有提到一件事,我正在编辑我的问题。
-
我认为您需要发布一个来自 Ruby 的示例 - 否则很难弄清楚发生了什么
标签: ruby-on-rails ruby r fastercsv