楼主: superhugo
7613 5

[问答] 如何用R实现循环保存? [推广有奖]

  • 0关注
  • 0粉丝

已卖:56份资源

硕士生

50%

还不是VIP/贵宾

-

威望
0
论坛币
7145 个
通用积分
3.3519
学术水平
8 点
热心指数
8 点
信用等级
7 点
经验
2602 点
帖子
131
精华
0
在线时间
145 小时
注册时间
2011-4-8
最后登录
2025-4-6

楼主
superhugo 发表于 2011-6-1 15:54:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
现在手头上有一个数据样本,共3650000个数据,想将其分割成100个样本文件,分别命名为x1.txt, x2.txt, x3.txt……其中x1.txt由第1~36500个数据组成;x2.txt由第36501~73000个数据组成,依此类推。觉得R处理这个问题应该挺简单的,不过编程过程中还是遇到了问题。程序代码如下:
for (i in 1:100)
{
j=(i-1)*36500+1
k=i*36500
for(l in j:k)
{
y[[l]]=x[[l]]
}
file=paste("y",i,".txt",sep="")
write.table(y,file,append=FALSE,sep="                 ")
}

输出结果没按照我事先设想的输出,不知道是什么原因?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:R实现 如何用 append paste write 保存

回帖推荐

楚韵荆风 发表于6楼  查看完整内容

file=paste("data", i,"txt")

zhangyangsmith 发表于2楼  查看完整内容

Something's wrong with the inner loop. In your script, y will be eventually assigned the same as x. I assume the results will be the first file has the first 1% of the data, the second has the first 2%,... Actually you may not need the inner loop at all. Consider the following code:

沙发
zhangyangsmith 发表于 2011-6-1 17:22:10
Something's wrong with the inner loop. In your script, y will be eventually assigned the same as x. I assume the results will be the first file has the first 1% of the data, the second has the first 2%,...

Actually you may not need the inner loop at all. Consider the following code:


  1. for (i in 1:100)
  2. {
  3. j=(i-1)*36500+1
  4. k=i*36500
  5. file=paste("y",i,".txt",sep="")
  6. write.table(x[[j:k]],file,append=FALSE,sep="                 ")
  7. }
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
superhugo + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
superhugo 发表于 2011-6-1 17:44:11
Thank you so much for your advice~~I will try your code on my computer

板凳
superhugo 发表于 2011-6-1 20:56:35
经检验 您提供的代码错误:
错误于x[[j:k]] : 最多只能选择一个元素。。。

应该将x[[j:k]] 改为x[j:k],这样就能达到目的了。
Thank you all the same:)

报纸
wzy1713 发表于 2011-6-10 13:54:58
在write里面用paste可以实现

地板
楚韵荆风 学生认证  发表于 2011-6-10 17:57:24
file=paste("data", i,"txt")
共享是一种彼此的快乐

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 07:45