# to get your current wd
getwd()
[1] "/Users/cameroncardona/Documents/r-bootcamp"
# to set your wd
setwd("~/Desktop")
Cameron J Cardona
In week 3, we cover two more tidyverse pacakges: readr and ggplot
All file paths in R can be referenced from your current working directory (wd). For example, if your wd is “C:/Users/You/Documents/folder1,” you can reference the file “C:/Users/You/Documents/folder1/myfile.xlsx” as “myfile.xlsx.” If your current wd is set to your “Downloads” folder, you will need to either change the directory, or use the full file path.
Here are some useful R codes pertaining to the working directory:
Files of all kinds can be read into R. The most common file types are likely .csv, .txt, and .xlsx. For .txt and .csv files, R has built in functions. There are also packages that read files inot R. One popular one is readr which has files for reading .txt, .csv and .tsv. The readr functions are typically quicker and can read larger datasets than the built in ones. For excel I like to use a package called readxl, but there are other packages that can read excel files. Pretty much any file type can be read into R. I have read .csv, .txt, .xlsx, .feather, .vcf, and files that are in the native formats of other stats programs like SAS, SPSS and STATA. The moreal of the story is that no matter the file type, someone has probably made a function that will read it into R for you.
These functions are commonly used to read data of different types in R:
File Type: | .txt | .csv | .xlsx |
---|---|---|---|
Functions: | read.table() readr::read_table() |
read.csv() readr::read_csv() |
readxl::read_excel() readxl::read_xlsx() |
Similarly to reading data into R, you can also export data from R in most formats. Remembering the functions is pretty easy. Both base R and readr have corresponding functions beginning with “write” instead of “read.” To write excel files, I recommend the package writexl and the function writexl::writexlsx(). Remember when saving from R, you need to include the file extension in the function. This is typically done at the end of the argument where you give the desired file path. In my experience this is usually second, after the dataframe you want to save.
File Type: | .txt | .csv | .xlsx |
---|---|---|---|
Functions: | write.table() readr::write_table() |
write.csv() readr::write_csv() |
writexl::write_xlsx() |