为什么在R中导致格式'%d无效?

sac*_*cvf 3 r

下面给出的代码是将二进制文件从float32转换为16b,比例因子为10.我得到%d无效的错误.

setwd("C:\\2001")
for (b in paste("data", 1:365, ".flt", sep="")) {
   conne <- file(b, "rb")
   file1<- readBin(conne, double(), size=4,  n=360*720, signed=TRUE)
   file1[file1 != -9999] <- file1[file1 != -9999]*10
   close(conne)
   fileName <- sprintf("C:\\New folder (11)\\NewFile%d.bin", b)
   writeBin(as.integer(file1), fileName, size = 2) 
}
Run Code Online (Sandbox Code Playgroud)

结果:

Error in sprintf("C:\\New folder (11)\\NewFile%d.bin",  : 
invalid format '%d'; use format %s for character objects
Run Code Online (Sandbox Code Playgroud)

%s按照R.But的建议使用 了1:365的文件完全是空的

eri*_*krf 6

%d是字符串中整数变量的占位符.因此,在使用时sprintf(%d, var),var必须是整数.

在您的情况下,变量b是一个字符串(或一个字符对象).所以,你使用占位符表示字符串变量%s.

现在,如果您的文件为空,那么代码中的其他地方一定有问题.你应该问另一个更具体的问题.