R 使用循环粘贴
R paste using a loop
我正在为软件生成一个输入文件。我 运行 在 paste0()
函数中出现了不一致。这是我的示例名称列表。
vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")
我想打印这个:
"Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);"
我试过了,但没有成功。不知怎的,“平等”走到了尽头。
paste0("Equal = ",
for(i in 1:length(vars)) {
Equal <- paste0("(",vars[i], ", Slope[0])", ",")
print(Equal)
})
[1] "(1234_AS_SA1_PCNS, Slope[0]),"
[1] "(2345_AS_SA2_UDA, Slope[0]),"
[1] "(3823_AS_SA3_CL, Slope[0]),"
[1] "Equal = "
此函数未正确使用 ","
s 以及末尾的 ";"
。
有什么想法吗?
谢谢。
我们可以在没有循环的情况下做到这一点,因为 paste
是矢量化的
cat(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"))
Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);
如果我们需要在末尾加双引号
cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"), FALSE))
"Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);"
或者可能是
cat(dQuote(paste0("Equal = ", paste(sprintf('"(%s, Slope[0])"', vars), collapse=",\n ", sep="")), FALSE))
"Equal = "(1234_AS_SA1_PCNS, Slope[0])",
"(2345_AS_SA2_UDA, Slope[0])",
"(3823_AS_SA3_CL, Slope[0])""
您不能使用 for 循环“扩展”成这样的函数参数。 特别是 不是试图 print
他们。这不是 Bash。
vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")
# Generate each "(<var>, Slope[0])" section
sections <- as.list(paste0("(", vars, ", Slope[0])"))
# Join the sections with ", "
sections_str <- do.call(paste, c(sections, sep = ", "))
# Construct the final string
result <- paste0("Equal = ", part2)
print(result)
我正在为软件生成一个输入文件。我 运行 在 paste0()
函数中出现了不一致。这是我的示例名称列表。
vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")
我想打印这个:
"Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);"
我试过了,但没有成功。不知怎的,“平等”走到了尽头。
paste0("Equal = ",
for(i in 1:length(vars)) {
Equal <- paste0("(",vars[i], ", Slope[0])", ",")
print(Equal)
})
[1] "(1234_AS_SA1_PCNS, Slope[0]),"
[1] "(2345_AS_SA2_UDA, Slope[0]),"
[1] "(3823_AS_SA3_CL, Slope[0]),"
[1] "Equal = "
此函数未正确使用 ","
s 以及末尾的 ";"
。
有什么想法吗? 谢谢。
我们可以在没有循环的情况下做到这一点,因为 paste
是矢量化的
cat(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"))
Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);
如果我们需要在末尾加双引号
cat(dQuote(paste0("Equal = ", paste("(", vars, ", Slope[0])",
collapse=",\n ", sep=""), ";"), FALSE))
"Equal = (1234_AS_SA1_PCNS, Slope[0]),
(2345_AS_SA2_UDA, Slope[0]),
(3823_AS_SA3_CL, Slope[0]);"
或者可能是
cat(dQuote(paste0("Equal = ", paste(sprintf('"(%s, Slope[0])"', vars), collapse=",\n ", sep="")), FALSE))
"Equal = "(1234_AS_SA1_PCNS, Slope[0])",
"(2345_AS_SA2_UDA, Slope[0])",
"(3823_AS_SA3_CL, Slope[0])""
您不能使用 for 循环“扩展”成这样的函数参数。 特别是 不是试图 print
他们。这不是 Bash。
vars <- c("1234_AS_SA1_PCNS","2345_AS_SA2_UDA", "3823_AS_SA3_CL")
# Generate each "(<var>, Slope[0])" section
sections <- as.list(paste0("(", vars, ", Slope[0])"))
# Join the sections with ", "
sections_str <- do.call(paste, c(sections, sep = ", "))
# Construct the final string
result <- paste0("Equal = ", part2)
print(result)