R 中的输出格式
我是 R 新手,尝试对多组数据进行相关性分析。我能够进行分析,但我试图弄清楚如何输出数据结果。我想要如下输出:
NAME,COR1,COR2
....,....,....
....,....,....
如果我可以将这样的文件写入输出,那么我可以根据需要对其进行后期处理。我的处理脚本如下所示:
run_analysis <- function(logfile, name)
{
preds <- read.table(logfile, header=T, sep=",")
# do something with the data: create some_col, another_col, etc.
result1 <- cor(some_col, another_col)
result1 <- cor(some_col2, another_col2)
# somehow output name,result1,result2 to a CSV file
}
args <- commandArgs(trailingOnly = TRUE)
date <- args[1]
basepath <- args[2]
logbase <- paste(basepath, date, sep="/")
logfile_pattern <- paste( "*", date, "csv", sep=".")
logfiles <- list.files(path=logbase, pattern=logfile_pattern)
for (f in logfiles) {
name = unlist(strsplit(f,"\\."))[1]
logfile = paste(logbase, f, sep="/")
run_analysis(logfile, name)
}
有没有一种简单的方法来创建空白数据框,然后逐行向其中添加数据?
I am new to R and trying to do some correlation analysis on multiple sets of data. I am able to do the analysis, but I am trying to figure out how I can output the results of my data. I'd like to have output like the following:
NAME,COR1,COR2
....,....,....
....,....,....
If I could write such a file to output, then I can post process it as needed. My processing script looks like this:
run_analysis <- function(logfile, name)
{
preds <- read.table(logfile, header=T, sep=",")
# do something with the data: create some_col, another_col, etc.
result1 <- cor(some_col, another_col)
result1 <- cor(some_col2, another_col2)
# somehow output name,result1,result2 to a CSV file
}
args <- commandArgs(trailingOnly = TRUE)
date <- args[1]
basepath <- args[2]
logbase <- paste(basepath, date, sep="/")
logfile_pattern <- paste( "*", date, "csv", sep=".")
logfiles <- list.files(path=logbase, pattern=logfile_pattern)
for (f in logfiles) {
name = unlist(strsplit(f,"\\."))[1]
logfile = paste(logbase, f, sep="/")
run_analysis(logfile, name)
}
Is there an easy way to create a blank data frame and then add data to it, row by row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看过 R 中用于将数据写入文件的函数吗?例如,
write.csv
。也许是这样的:Have you looked at the functions in R for writing data to files? For instance,
write.csv
. Perhaps something like this:我喜欢使用 foreach 库来做这类事情:
它的作用是在每次调用
run_analysis
时生成一行输出,将它们绑定到一个名为dat
的表中(对foreach
调用的.combine="rbind"
部分会导致r
owbind
ing)。然后你可以使用write.csv
来获得你想要的输出。I like using the foreach library for this sort of thing:
What this does is to generate one row of output on each call to
run_analysis
, binding them into a single table calleddat
(the.combine="rbind"
part of the call toforeach
causesr
owbind
ing). Then you can just usewrite.csv
to get the output you want.