Read multiple .gpx files

核能气质少年 提交于 2019-12-05 09:13:53

Fred,

After installing GPSBabel and updating the PATH variable, your code snippet ran fine. I have two objects names test1.gpx_NA_NA and test2.gpx_NA_NA with three observations of 28 variables. Is that right? I assume the NA bit in the file names is due to how you are defining visit.id and my test file names not fitting into that paradigm.

Have you tried this on a fresh instance of R?

FWIW, I would probably read all of these files into a single list object. I find dealing with a list object easier than having lots of different objects floating around. For example,

files <- dir(pattern = "\\.gpx")
#Replace all space characters with a "_". Replace with the character of your choice.
lapply(files, function(x) file.rename(from = x, to = gsub("\\s+", "_", x)))

#Reread in files with better names:
files <- dir(pattern = "\\.gpx")
out <- lapply(files, function(x) readGPS(i = "gpx", f = x, type = "w"))
names(out) <- files

and out is now a list of 2, where each object is a data.frame with the name of the file that it was associated with previously. Using something from the *apply family has the benefit leaving a clean working space behind as well. Using the for-loop results in x, temp, and visit.id hanging out afterwords. You could wrap them up into a function call, but just using lapply will be more straight forward I think.

Turns out the code was fine, the problem is with the file names. GPSBabel does not like names with white spaces. So "1_San José Baldi_Pernam_14_sep.gpx" is a problem, "1_San_José_Baldi_Pernam_14_sep.gpx" is not.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!