Visit the R Project website :https://cran.r-project.org/ and download the latest version of R for your operating system. Follow the installation instructions.
Visit the R Studio website and download the free version of R Studio Desktop. Follow the installation instructions.
Open R Studio.
Select File > New Project > New Directory.
Choose “New Project.”
Enter a name for your project and choose the location where you want to create the project directory.
Click “Create Project.”
R Studio will create a new project with an associated .Rproj file. This file stores project-specific settings, including the working directory.
Now that you have an R project, the working directory is automatically set to the root directory of your project.
You can install packages in R using the
install.packages()
function.
Let’s install the tidyverse package, which includes many useful data manipulation and visualization packages.
install.packages("tidyverse")
Once installed, load the package using the library()
function.
library(tidyverse)
Create a new R script file (.R) in R Studio by selecting File > New File > R Script. You can write and execute your R code in this script.
The here
package helps manage file paths, making it
easier to locate your datasets even when you move your script or
project.
Install the here package:
install.packages("here")
Now, use it in your script:
# Load the here package
library(here)
# Assuming your dataset is in a 'data' folder within your working directory
data_path <- here("data", "your_dataset.csv")
# Load the dataset using read.csv() or any other appropriate function
your_dataset <- read.csv(data_path)
Replace “your_dataset.csv” with the actual name of your dataset file.
Remember to adapt the paths and file names based on your project structure. The paths will be relative to your project directory.
This approach ensures that your code remains portable, and you can share your project with others without worrying about absolute file paths.