Need help with R Code

Have a code that creates a list of file names with a directory.

dataframe1 =list.files(path = "C:/Users/xxxxxxx/OneDrive/Documents/test")

thats fine. it has 10 files with different file formats.

I have an excel spread sheet that has the names of those files but not their file extension. I want to write a code that will use the names in the excel spread sheet, compare with the dataframe 1, then write a hyperlink column in the excel spread.

Can anyone help me out on this? Been kicking my butt. I'm sure its simple but my mind isn't working well of late.

example.

name column in excel = 100

file name could be 100.txt or 100.pfd

final result must be a new column in the excel sheet that writes a hyper link to C:/Users/xxxxxxx/OneDrive/Documents/test/100.(filetype)

Then saves the excel spreadsheet.

The last part of your post is not clear. In terms of extracting file extension, you can use the list.files function,specify the full.names argument to TRUE . The function will return a list of files in your specified directory. Thereafter, you can use the stringr package to extract and replace the current file extension to your preferred extension type. Lastly, you store the changed extensions in a new column. If you need to copy the new file extensions you can use file.copy(from = dataframe_1$current_names, to = dataframe_1$new_names)

library(tidyverse)

dataframe_1 <- data.frame(current_names = list.files(path = "C:/Users/xxxxxxx/OneDrive/Documents/test",
                                                    full.names = TRUE))

dataframe_1 <- dataframe_1 %>%
mutate(new_names = str_replace(string = current_name, pattern="[[:punct:]]\\w{1,}$", replacement = ".txt"))
1 Like

So step by step here is what I want to do.

  1. Read column in excel spreadsheet. E.g. (filename column. Document 1 (no file type is mentioned in this cokumn))

  2. Create a list of all documents within a folder. Full names with file type.

  3. For I in excel spreadsheet file name. If I is in document list, write document full name to end of two for I in excel spreadsheet.

Does that make more sense?