我有一个类似于 this 的数据。我的代码如下 只需计算一些值并将输出向量绑定(bind)到 原始数据框。
options(width=200)
args<-commandArgs(trailingOnly=FALSE)
dat <- read.table("http://dpaste.com/89376/plain/",fill=T);
problist <- c();
for (lmer in 1:10) {
meanl <- lmer;
stdevl <- (0.17*sqrt(lmer));
err_prob <- pnorm(dat$V4,mean=meanl, sd=stdevl);
problist <- cbind(problist,err_prob);
}
dat <- cbind(dat,problist)
#print(dat,row.names=F, column.names=F,justify=left)
# Why this breaks?
write(dat, file="output.txt", sep="\t",append=F);
关于上述内容我有几个问题:
但是为什么上面的“write()”函数会给出这个错误。有办法解决吗?
cat(list(...)、file、sep、fill、labels、append) 中的错误: 参数 1(类型“list”)无法由“cat”处理 调用: write -> cat 执行停止
数据框中绑定(bind)向量的名称被添加为所有 10 个新的“errprob” 列。有没有办法将它们命名为“errprob1”、“errprob2”等?
最佳答案
首先,不需要分号,R 知道行尾是一个中断。
for (lmer in 1:10){
meanl <- lmer
stdevl <- (0.17*sqrt(lmer))
err_prob <- pnorm(dat$V4,mean=meanl, sd=stdevl)
problist <- cbind(problist,err_prob)
}
colnames(problist)<-paste("errorprob",1:10,sep="")
dat <- cbind(dat,problist)
write.table(dat, file="output.txt", sep="\t",append=F)
我相信您正在寻找 write.table 函数
使用 colnames 函数
关于r - 在数据帧 : Writing to File and Naming Binded Vector in R 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1377248/