-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.r
53 lines (47 loc) · 1.83 KB
/
utils.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#' Import all excel files in a given directory as a single dataframe
#'
#' @return a dataframe with imported data
#' @param dir name of the directory with files to import
#' @param sheet which sheet to import
#' @param pattern regexp that matches filenames to be imported
#' @export
ExcelToDF <- function(dir = ".", sheet = 1, pattern = ".xlsx") {
filenames <- list.files(path = dir,
pattern = pattern,
full.names = TRUE)
if (length(filenames) == 0) {
cat("No files matching these criteria\n")
} else if (length(filenames) == 1) {
dfAll <- read_excel(filenames, sheet = sheet)
return(data.frame(dfAll))
} else {
df.list <- lapply(filenames,
function(x) read_excel(path = x,
sheet = sheet))
if (length(unique(lapply(df.list, function(x) names(x)))) != 1) {
cat("The excel files can not be merged as they are
not the same format. The following files are
selected based on your pattern:\n")
return(filenames)
} else { dfAll <- Reduce(function(x, y) merge(x, y, all = TRUE),
df.list)
return(dfAll)
}
}
}
#' Convert GPS coordinates from SWEREF99 to WGS84
#'
#' @return a data frame with the same information in input, but in
#' WGS84
#' @param data dataframe or matrix with input data
#' @param Latitude Column in data that holds latitude data
#' @param Longitude Column in data that holds longitude data
GPSConvert <- function(data, Latitude, Longitude) {
SWEREF99 <- 3006
WGS84 <- 4326
p1 <- sf::st_as_sf(data,
coords = c(Longitude, Latitude),
crs = SWEREF99)
p2 <- sf::st_transform(p1, WGS84)
p2
}