check last three characters of a file in r

You can use the substr and nchar functions in base R to extract the last three characters of a filename, and then compare it to a specific string using the identical function. Here's an example code snippet:

main.r
# here's an example filename
filename <- "data.csv"

# extract the last three characters using substr and nchar
last_three_chars <- substr(filename, nchar(filename)-2, nchar(filename))

# check if the last three characters are 'csv'
if(identical(last_three_chars, "csv")) {
  cat("The file ends with 'csv'\n")
} else {
  cat("The file does not end with 'csv'\n")
}
365 chars
13 lines

This code first sets the filename to "data.csv". It then uses substr and nchar to extract the last three characters of the filename, which is "csv". The identical function is then used to compare the extracted last three characters to the string "csv". Since the comparison is true, the code will print "The file ends with 'csv'". You can replace "data.csv" with the path to your own file to check its last three characters.

gistlibby LogSnag