Read Csv R Windows Not Home Directory

R: How to create, delete, move, and more with files

Though Python is usually thought of over R for doing organization administration tasks, R is actually quite useful in this regard. In this mail nosotros're going to talk almost using R to create, delete, motility, and obtain data on files.

How to become and change the current working directory

Earlier working with files, it's usually a practiced idea to first know what directory you're working in. The working directory is the folder that any files you lot create or refer to without explicitly spelling out the total path fall inside. In R, you can figure this out with the getwd function. To change this directory, you can use the aptly named setwd function.

                  # become current working directory getwd()  # set working directory setwd("C:/Users")              

Creating Files and Directories

A new binder, or directory, tin be created in R using the dir.create office, similar this:

dir.create("new_folder")              

You just need to supersede "new_folder" with whatever proper name yous cull. If you don't write out the total path of this new directory, it will get created into any the current working directory is i.eastward. the value of getwd().

Similarly, creating a bare file can exist done with file.create.

                  file.create("new_text_file.txt") file.create("new_word_file.docx") file.create("new_csv_file.csv")              

With this in mind, creating lots of files apace is made easy. For example, the one-liner below will create 100 empty text files:

                  sapply(paste0("file", 1:100, ".txt"), file.create)              

Copying a file / folder

Copying a file tin be done using file.copy.

                  file.copy("source_file.txt", "destination_folder")              

With file.copy, the get-go parameter is the proper noun of the file to be copied; the second is the destination folder that you want to copy the file to. If the file copies successfully, the function will return True — otherwise, it returns FALSE.

How to listing all the files in a directory

The simplest way of listing all the files in a directory with R is by calling list.files.

                  # list all files in current directory listing.files()  # list all files in some other directory listing.files("C:/path/to/somewhere/else")              

Calling list.files with no additional parameters will only list the files and folders directly within the directory — i.due east. it doesn't listing the files within any sub-folder unless you tell information technology to do and then. This can be done like this:

                  listing.files("C:/path/to/somewhere/else", recursive = TRUE)              

Note, adding the "recursive = TRUE" flag may cause the function to run for a longer period of time if the directory has a large number of sub-folders and files (e.g. running listing.files("C:/", recursive = True)).

An additional point — running default list.files doesn't list the full path names of the files. We can set the parameter, full.names, to TRUE to get the full path names.

                  listing.files("C:/path/to/somewhere/else", full.names = TRUE, recursive = True)              

list.files tin also utilise a filter internally to the files y'all desire to list. For case, the R code below will list all of the CSV files in a directory (similar to "ls | grep .csv" in Linux)

                  # list all CSV files not-recursively list.files(blueprint = ".csv")  # listing all CSV files recursively through each sub-folder listing.files(pattern = ".csv", recursive = TRUE)              

The to a higher place logic tin can be really useful if yous want to read in all of the CSV files inside a given directory. For instance, suppose you have a list of CSV'south in a folder, and you want to produce a single data frame (provided they each accept the same layout) from all the files. You lot can achieve this in a couple lines of lawmaking:

                  # read in all the CSV files all_data_frames <- lapply(list.files(pattern = ".csv"), read.csv)  # stack all information frames together single_data_frame <- Reduce(rbind, all_data_frames)              

How to get created / modified times and other details well-nigh files

fileSnapshot

Another style of getting the files in a directory is using the part, fileSnapshot. fileSnapshot will as well requite y'all additional details near the files. This office returns a list of objects.

                  # get file snapshot of current directory snapshot <- fileSnapshot()  # or file snapshot of another directory snapshot <- fileSnapshot("C:/some/other/directory")              

fileSnapshot returns a list, which hither we will but phone call "snapshot". The almost useful slice of information can be garnered from this by referencing "info":

                  snapshot$info              

Hither, snapshot$info is a data frame showing information about the files in the input folder parameter. Its headers include:

  • size ==> size of file
  • isdir ==> is file a directory? ==> TRUE or FALSE
  • mode ==> the file permissions in octal
  • mtime ==> concluding modified time stamp
  • ctime ==> time stamp created
  • atime ==> time stamp last accessed
  • exe ==> type of executable (or "no" if not an executable)
  • file.info

    file.info is similar to fileSnapshot, except that it returns a unmarried record of information corresponding to an input file. For instance, the code beneath will return the fields above (size, isdir, mode, mtime etc.) for the specific file, "some_file.csv":

                      file.info("some_file.csv")              

    file.ctime

    If yous want to get merely the created fourth dimension stamp of a file, call file.ctime:

                      file.ctime("C:/path/to/file/some_file.txt")              

    file.mtime

    Getting the last modified fourth dimension stamp is similar to to a higher place, except we utilize file.mtime:

                      file.mtime("C:/path/to/file/some_file.txt")              

    How to delete files

    Files can be deleted with R using unlink. Deleting a single file is as unproblematic as passing the file's name to this function.

    To delete a directory, y'all have to add the parameter recursive = True.

                      # delete a file unlink("some_file.csv")  # delete another file file.remove("some_other_file.csv")  # delete a directory -- must add recursive = True unlink("some_directory", recursive = TRUE)              

    With unlink, nosotros tin delete the 100 text files we created above with file.create — also in just ane line of code.

                      sapply(paste0("file", 1:100, ".txt"), unlink)              

    How to cheque if a file or directory exists

    You tin check if a file exists, using the file.exists function.

                      # bank check if a file exists file.exists("C:/path/to/file/some_file.txt")  # check if a folder exists file.exists("C:/path/to/file/some_folder")  # alternatively, check if a folder exists with dir.exists dir.exists("C:/path/to/file/some_folder")              

    Running file.exists will render TRUE whether an existing file is a directory or not, whereas dir.exists volition return Truthful if and only if the input value exists and is a directory.

    How to get the base name of a file

    Getting the base proper noun of a file can be done using the basename part:

                      basename("C:/path/to/file.txt")              

    The higher up lawmaking will return "file.txt"

    How to get the directory name of a file

    Tweaking the code to a higher place, we can get the directory of a file similar this:

                      dirname("C:/path/to/file.txt")              

    This will return "C:/path/to"

    How to get a file'due south extension

    Getting a file'southward extension can be done using the file_ext part from the tools parcel.

                      library(tools)  file_ext("C:/path/to/file.txt") # returns "txt"  file_ext("C:/path/to/file.csv") # returns "csv"              

    How to physically open a file

    To physically open, or launch, a file, utilize the beat out.exec or file.show functions:

                      # apply trounce.exec... shell.exec("C:/path/to/file/some_file.txt")  # or file.testify to launch a file file.show("C:/path/to/file/some_file.txt")              

    This tin can be really handy if yous're modifying a department of code that writes over the same file, and yous want to open it to bank check some results without having to manually practise and so.

    How to open a file selection window

    To open a file pick window, you lot can run file.choose():

                      file.choose()              

    Running this control will return the proper noun of the file selected by the user.

    How to movement a file

    As of this writing, in that location is not a congenital-in base R part to directly move a file from ane place to another, but this can be accomplished using the filesstrings package, and its function file.motility:

                      library(filesstrings)  file.move("C:/path/to/file/some_file.txt", "C:/some/other/path")              

    Here, the first argument is the name of the file you lot want to move. The second argument is the destination directory.

    Click here to read other R articles of mine.

    mannsnarstrabest.blogspot.com

    Source: http://theautomatic.net/2018/07/11/manipulate-files-r/

    0 Response to "Read Csv R Windows Not Home Directory"

    Publicar un comentario

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel