我编写了一个函数,允许我从剪贴板中获取四列数据,拆分它,添加一个额外的列,重新组合它,然后将其导出为文本文件。
xyztoinp <- function(x) {
x <- read.table(file="clipboard")
a <- x[,-1]
b <- x[,1]
c <- ifelse(b == "C", 6,
ifelse (b == "O", 8,ifelse (b == "H", 1, 3)))
x <- cbind(b,c,a)
write.table(x, file="IRCoutput", quote = FALSE,
sep = "\t", row.names = FALSE, col.names = FALSE)
}
我有两个问题:-
是否可以创建一个文件,其中包含我使用此函数创建的表格,以便将表格粘贴到某些文本的中间?
例如
text_text_text_text_text_text_text_
text_text_text_text_text_text_text_
Neatly formated table
text_text_text_text_text_text_text_
text_text_text_text_text_text_text_
其次,我尝试修改函数,使其将文件名作为变量;IE,xyztoinp <- function(x, NewFileName) {
等等......但这并没有产生带有新文件名的输出;有谁知道这是怎么做到的?
这是我的工作代码,
xyztoinp <- function(x) {
x <- read.table(file="clipboard")
a <- x[,-1]
b <- x[,1]
c <- ifelse(b == "C", 6,
ifelse (b == "O", 8,
ifelse (b == "H", 1, 3)))
x <- cbind(b,c,a)
zz <- file("NewFile.inp", "w")
cat("text", "text",
"text",
"",
"text",
"text",
"text", file = zz, sep = "\n")
write.table(x,文件 = zz,报价 = FALSE,
sep = "\t",row.names = FALSE,col.names = FALSE)
cat("text",文件=zz)
关闭(zz)
}
谢谢大家,尤其是@Greg。ps 无法更改函数中的文件名,但我对上述内容感到满意。