使用 R 将 "commas" 替换为 "dots" 多个 .asc
use R to replace "commas" with "dots" for multiple .asc
我有大约 1000 个 ascii 文件,我需要在其中删除 header,然后用点替换逗号。
我已经发现这个答案对于循环过程和删除 header 非常有帮助:(use R to remove header (6 lines) from .asc file (ESRI ascii grid) and export)
setwd("c:/temp/hdr/ascii")
newdir <- "c:/temp/hdr/ascii_no_hdr/"
files <- dir(pattern="*.asc")
for (my.file in files){
i <- read.table(my.file,skip=16,sep="")
write.table(i,file=paste(newdir,my.file,sep=""),sep="",row.names=FALSE,col.names=FALSE, quote=FALSE)
}
如何将 gsub 函数集成到代码中?
抱歉,我是 R 的新手。
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22
1 33,45 33,45 33,64 32,82 32,95 33,52 33,47 33,96 34,28 34,04 34,42 34,26 34,00 33,64 33,60 33,37 33,32 32,59 32,15 31,52 31,20 30,83
2 32,15 32,61 33,24 32,99 32,88 33,22 33,68 34,09 34,59 34,81 34,51 35,25 34,77 34,55 34,13 33,66 33,51 32,92 32,84 32,17 31,87 31,15
3 32,25 32,10 33,05 32,88 32,69 33,03 33,28 34,02 34,40 34,96 35,43 35,55 35,57 35,36 35,06 34,64 34,43 34,43 34,15 33,39 33,13 32,69
4 31,92 31,73 32,31 32,94 32,23 32,44 32,95 33,54 34,17 34,74 35,26 35,58 35,66 36,00 35,81 35,43 35,15 35,04 34,59 34,34 34,17 34,34
5 32,00 31,85 31,52 32,86 32,40 32,27 32,99 33,52 33,89 34,77 35,34 35,58 36,13 36,15 36,07 36,00 35,57 35,64 35,19 34,85 34,77 34,72
6 32,06 32,08 31,77 32,61 32,32 32,27 32,55 33,39 34,06 34,93 35,68 36,04 36,39 36,50 36,37 36,37 36,19 35,72 35,58 35,51 35,04 35,08
7 32,17 32,27 31,50 32,55 32,57 31,79 32,80 33,16 34,09 34,79 35,64 35,87 36,26 36,52
[已删除 header 的其中一个文件的一部分]
我们可以在 read.table
中指定 dec=","
以正确地将文件中的 ,
更改为 .
,即在您的代码中然后 write
OP 代码中显示的已更改文件。
setwd("c:/temp/hdr/ascii")
newdir <- "c:/temp/hdr/ascii_no_hdr/"
files <- dir(pattern="*.asc")
for (my.file in files){
i <- read.table(my.file,skip=16,sep="", dec=",") ###here
write.table(i,file=paste(newdir,my.file,sep=""),
sep="",row.names=FALSE,col.names=FALSE, quote=FALSE)
}
我有大约 1000 个 ascii 文件,我需要在其中删除 header,然后用点替换逗号。
我已经发现这个答案对于循环过程和删除 header 非常有帮助:(use R to remove header (6 lines) from .asc file (ESRI ascii grid) and export)
setwd("c:/temp/hdr/ascii")
newdir <- "c:/temp/hdr/ascii_no_hdr/"
files <- dir(pattern="*.asc")
for (my.file in files){
i <- read.table(my.file,skip=16,sep="")
write.table(i,file=paste(newdir,my.file,sep=""),sep="",row.names=FALSE,col.names=FALSE, quote=FALSE)
}
如何将 gsub 函数集成到代码中? 抱歉,我是 R 的新手。
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22
1 33,45 33,45 33,64 32,82 32,95 33,52 33,47 33,96 34,28 34,04 34,42 34,26 34,00 33,64 33,60 33,37 33,32 32,59 32,15 31,52 31,20 30,83
2 32,15 32,61 33,24 32,99 32,88 33,22 33,68 34,09 34,59 34,81 34,51 35,25 34,77 34,55 34,13 33,66 33,51 32,92 32,84 32,17 31,87 31,15
3 32,25 32,10 33,05 32,88 32,69 33,03 33,28 34,02 34,40 34,96 35,43 35,55 35,57 35,36 35,06 34,64 34,43 34,43 34,15 33,39 33,13 32,69
4 31,92 31,73 32,31 32,94 32,23 32,44 32,95 33,54 34,17 34,74 35,26 35,58 35,66 36,00 35,81 35,43 35,15 35,04 34,59 34,34 34,17 34,34
5 32,00 31,85 31,52 32,86 32,40 32,27 32,99 33,52 33,89 34,77 35,34 35,58 36,13 36,15 36,07 36,00 35,57 35,64 35,19 34,85 34,77 34,72
6 32,06 32,08 31,77 32,61 32,32 32,27 32,55 33,39 34,06 34,93 35,68 36,04 36,39 36,50 36,37 36,37 36,19 35,72 35,58 35,51 35,04 35,08
7 32,17 32,27 31,50 32,55 32,57 31,79 32,80 33,16 34,09 34,79 35,64 35,87 36,26 36,52
[已删除 header 的其中一个文件的一部分]
我们可以在 read.table
中指定 dec=","
以正确地将文件中的 ,
更改为 .
,即在您的代码中然后 write
OP 代码中显示的已更改文件。
setwd("c:/temp/hdr/ascii")
newdir <- "c:/temp/hdr/ascii_no_hdr/"
files <- dir(pattern="*.asc")
for (my.file in files){
i <- read.table(my.file,skip=16,sep="", dec=",") ###here
write.table(i,file=paste(newdir,my.file,sep=""),
sep="",row.names=FALSE,col.names=FALSE, quote=FALSE)
}