gpt4 book ai didi

r - 帮助 R 中的 sprintf 函数

转载 作者:行者123 更新时间:2023-12-04 06:47:15 24 4
gpt4 key购买 nike

我有以下 R 代码:

df <- xmlToDataFrame(/Users/usr/Desktop/shares.xml)
df$timeStamp <- strptime(as.character(df$XTimeStamp), "%H:%M:%OS")
df$SharePrice <- as.numeric(as.character(df$SharePrice))
sapply(df, class)
options("digits.secs"=3)
diff <- diff(df$SharePrice)
diff
sink (file="c:/xampp/htdocs/data.xml", type="output",split=FALSE)
cat("<graph caption=\"Share Price Data Wave\" subcaption=\"For Company's Name\" xAxisName=\"Time\" yAxisMinValue=\"0\" yAxisName=\"Price\" decimalPrecision=\"5\" formatNumberScale=\"0\" numberPrefix=\"\" showNames=\"1\" showValues=\"0\" showAlternateHGridColor=\"1\" AlternateHGridColor=\"ff5904\" divLineColor=\"ff5904\" divLineAlpha=\"20\" alternateHGridAlpha=\"5\">\n")
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp, df$SharePrice, diff))

我正在以 FusionChart Free 可以读取的格式创建一个新的 xml 文件,并且我试图将消息放在 hoverText 区域中。但是,当我运行命令时,出现以下错误:
Error in sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n",  : 
arguments cannot be recycled to the same length

当我查看差异时,它比 SharePrice 少一个差异(显然是因为从点 1 到点 1 的差异为零),那么如何在 sprint 函数中说明这一点(如果我省略了差异)?

最佳答案

要么去掉df$XTimeStamp的第一个元素和 df$SharePrice或添加 NA作为 diff 的第一个元素.

cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp[-1], df$SharePrice[-1], diff))

或者
cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp, df$SharePrice, c(NA,diff)))

关于r - 帮助 R 中的 sprintf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3570700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com