for (i in 3:52){
cat(" ID,NAME,LAT,LONG,ELEVATION
1,25_i_PCP,39.269,-92.127,261.000
2,32_i_PCP,39.241,-92.160,263.000
3,34_i_PCP,39.223,-92.141,264.000
4,38_i_PCP,39.298,-92.083,243.000
5,201_i_PCP,39.228,-92.119,263.000
",file="pcpfork_i.txt",sep="\n",append=TRUE)}"
我希望使用for循环创建50个不同的.txt
文件,将i
从3替换为52.
答案 0 :(得分:4)
以下是几个选项。首先,请注意。您在append = TRUE
的通话中不需要sep = "\n"
或cat()
,因为没有任何内容可以追加或分开。这些都是分开的文件。
选项1 :在您的文字中,您可以将i
替换为%d
,将其称为tmp
,然后通过sprintf()
循环运行tmp <- "ID,NAME,LAT,LONG,ELEVATION
1,25_%d_PCP,39.269,-92.127,261.000
2,32_%d_PCP,39.241,-92.160,263.000
3,34_%d_PCP,39.223,-92.141,264.000
4,38_%d_PCP,39.298,-92.083,243.000
5,201_%d_PCP,39.228,-92.119,263.000"
for (i in 3:52) {
cat(
do.call(sprintf, c(tmp, as.list(rep(i, 5)))),
file = sprintf("pcpfork_%d.txt", i)
)
}
{1}}。
gsub()
选项2:保留原始文本并使用tmp <- "ID,NAME,LAT,LONG,ELEVATION
1,25_i_PCP,39.269,-92.127,261.000
2,32_i_PCP,39.241,-92.160,263.000
3,34_i_PCP,39.223,-92.141,264.000
4,38_i_PCP,39.298,-92.083,243.000
5,201_i_PCP,39.228,-92.119,263.000"
for (i in 3:52) {
cat(
gsub("_i_", paste0("_", i, "_"), tmp, fixed = TRUE),
file = sprintf("pcpfork_%d.txt", i)
)
}
进行替换。
fonts[2] = new Font(Font.HELVETICA, 8, Font.BOLD);
Paragraph paranumber = new Paragraph(
fonts[2].getCalculatedLeading(1),
headingText.trim()
+ " ", fonts[0]);
Paragraph para = new Paragraph(
fonts[0].getCalculatedLeading(1), contentText.trim(), fonts[0]);
para.setAlignment(Element.ALIGN_JUSTIFIED);
para.setSpacingAfter(3f);
//Now adding the para to paraNumber that is having the heading and expecting
//that it will be added very after the heading, but this does not show correct
//result, formatting issue.
paranumber.add(para);
mct.addElement(paranumber);
那些应该让你朝着正确的方向前进。