I'm adapting the sapply function from someone else's script to pull in multiple spreadsheets and apply the site number to each item in the list (e.g. 124001B_calibrated.csv should become 124001B). My problem is that the code below is returning the entire file location (N:/Projects and project proposals/.... etc) instead of just the site code. Can anyone tell me what I am doing wrong? Does it have something to do with the [[
shortcut? thank you in advance.
#Get site number from filenames
sitenames_Sites <- gsub("_calibrated.*", "", fileNames)
sitenames_Sites <- sapply(strsplit(sitenames_Sites, split = "_"), `[[`, 5)
#List spreadsheets as elements of a list
list_spreadsheets <- lapply(fileNames, read.csv, header=TRUE)
#Name element of the list according to their site code
names(list_spreadsheets) <- sitenames_Sites
#Merge individual datasets from list
Merged_Sites <- do.call("rbind", list_spreadsheets)
#Turn rownames (which contain site number) to a column
Merged_Sites <- tibble::rownames_to_column(Merged_Sites, "SiteNumber")
EDIT: this code worked
#Get site number from filenames
sitenames_Sites <- gsub("_calibrated.*", "", fileNames)
sitenames_Sites <- sapply(strsplit(sitenames_Sites, split = "\\\\|_calibrated"), [[, 5)
#Merge individual datasets from list
Merged_Sites <- do.call("rbind", list_spreadsheets)
#Turn rownames (which contain site number) to a column
Merged_Sites <- tibble::rownames_to_column(Merged_Sites, "SiteNumber")
#Remove rownumber after '.' symbol
Merged_Sites$SiteNumber <- gsub("\\..*", "", Merged_Sites$SiteNumber)