When coding in R, you will often need to input datasets to work with! The easiest ways to do so are either from a .csv file or a .txt file. To do this, you can use the read.csv() and read_table() functions, respectively. The following demonstrates these functions using a hypothetical "hospital_data" dataset.
To output a file from R, use the syntax sink("FileName.FileType").
File Input:
#If the dataset is already loaded into the R directoryread.csv("hospital_data.csv")read_table("hospital_data.txt")#To add a new dataset from machine downloads to directory (Mac)read.csv("/users/username/Downloads/hospital_data.csv")read_table("/users/username/Downloads/hospital_data.txt")#To add a new dataset from machine desktop to directory (Windows)read.csv("C:\\Users\\username\\Desktop\\hospital_data.csv")read_table("C:\\Users\\username\\Desktop\\hospital_data.txt")#Note that forward slashes are used on Mac and backwards slashes are used by Windows
File Output:
#To output a file as a .txt file:sink("hospital_data.txt")#To output a file as a .csv file:sink("hospital_data.csv")