r - 在数据帧 : Writing to File and Naming Binded Vector in R 上

标签 r vector dataframe writefile

我有一个类似于 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);

关于上述内容我有几个问题:

  1. 但是为什么上面的“write()”函数会给出这个错误。有办法解决吗?

    cat(list(...)、file、sep、fill、labels、append) 中的错误: 参数 1(类型“list”)无法由“cat”处理 调用: write -> cat 执行停止

  2. 数据框中绑定(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)

  1. 我相信您正在寻找 write.table 函数

  2. 使用 colnames 函数

关于r - 在数据帧 : Writing to File and Naming Binded Vector in R 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1377248/

相关文章:

python - 从 Pandas 字典中选择多个 DataFrame

r - ggplot 中虚拟变量的分位数回归误差

java - 用 vector 填充二维数组时出现 NullPointerException

R 在特定值之前添加空行

python Pandas : how to fast process the value in columns

python - pygame 敌人朝向玩家的二维运动,如何计算 x 和 y 速度?

r - 如何在 R 中将多个 csv 文件读取到单个数据框中?

r - 如何在R中将离散比率数据转换为序数数据?

r - Gamma 障碍(两部分)模型和零膨胀 Gamma 模型之间有区别吗?

c++ - 作用于整数 vector 的循环表现异常