开发者

Writing to a filename that varies depending on a variable in R

开发者 https://www.devze.com 2023-03-02 19:47 出处:网络
Using R in Windows 7 I have a program that creates a big matrix called patients.It also has a variable called filenum.I would like to write the file to a table that varies based on filenum.For instan

Using R in Windows 7

I have a program that creates a big matrix called patients. It also has a variable called filenum. I would like to write the file to a table that varies based on filenum. For instance, if filenum = 1, I'd like it to write out to

"c:\personal\output1"

How can I do this?

I've been pla开发者_JAVA百科ying with paste and a little with gsub, but I can't get this to work

Thanks in advance

Peter


You could do this via lapply(split(), write.table, ...) or the equivalent function from plyr, but it would probably be fastest and cleanest to do this with a for loop. Something like:

for(fnum in unique(patients[,"filenum"])) {
  set <- which(patients[,"filenum"] == fnum)
  write.table(patients[set,], paste("c:/personal/output",fnum,sep=""))
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号