The code given below is to convert binary files from float32 to 16b with scale factor of 10. I am getting error of invalidation of %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)
}
Result:
Error in sprintf("C:\\New folder (11)\\NewFile%d.bin", :
invalid format '%d'; use format %s for character objects
I used %s
as suggested by R.But the files from 1:365 were totally empty
The %d
is a placeholder for a integer variable inside a string. Therefore, when you use sprintf(%d, var)
, var
must be an integer.
In your case, the variable b
is a string (or a character object). So, you use the placeholder for string variables, which is %s
.
Now, if your files are empty, there must be something wrong elsewhere in your code. You should ask another question more specific to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With