I have a large group of files. I want to delete all the text within these files and paste a new string which I call mytext
.
I'm new to R, but I've written an R code that nearly does the job perfectly. It's very messy and I'm sure I'm taking unnecessary steps. Furthermore, it doesn't quite delete all the text like I want, and it leaves quotation marks, which I can't have.
temp = list.files(pattern="*.Hdr")
for(x in 1:length(temp)) {
data = read.csv(temp[x], header = FALSE)
data <- data[NULL, NULL]
write.csv(data, file = temp[x]) ## output new file
}
temp = list.files(pattern = "*.Hdr")
mytext = "nrows 4096
ncols 8192
nbands 1
nbits 16
pixeltype signedint
byteorder M
layout dat
ulxmap -130.517083333328
ulymap 58.2329166666644
xdim 0.00833333333
ydim 0.00833333333" ## This is the variable I want to keep
for(x in 1:length(temp)) {
data = read.csv(temp[x], header = FALSE)
data = paste(mytext)
write.csv(data, file = temp[x]) ## output new file
}
My resulting tables all look like this:
"","x"
"1","nrows 4096
ncols 8192
nbands 1
nbits 16
pixeltype signedint
byteorder M
layout dat
ulxmap -130.517083333328
ulymap 58.2329166666644
xdim 0.00833333333
ydim 0.00833333333"
Which is nearly perfect, but I don't want:
"","x",
"1",
Nor do I want the quotation marks around mytext
. I'd appreciate someone with more experience pointing out my mistake.
What you are looking at are the column headers and row names. You can turn those off along with the quoting with
write.csv(data, file = temp[x], header=F, row.names=F, quote=F)