Basics R

Lam Fui Ching

15-5-2023

Get Started

Log a ticket with ICT Support Portal to download and install the following:-

RStudio Layout

Four RStudio Panels

1. Source

2. Console

3. Environment/History/Connections/Tutorial

To view:

4. Files/Plots/Packages/Help

To view:

Set up a working directory

Two options:-

Option 1: Script

setwd("Path R scripts and/or data file are stored")

setwd("~/Powerbi Reports/ASCOF/data")

Option 2:

Running script

Firstly, move the cursor anywhere in the start of the line, then:-

  1. press key Ctrl-Enter or
  2. click on the Run button at the top of the source panel.

This not only runs the current command, but also moves the cursor to the next command, making it easy to walk step-by-step through the script.

It is highlight all the code you want to run and then key Ctrl-Enter or click Run. (Be careful: you can highlight less than a full command, and R will try to execute only what been highlighted!)

Import Data

Import a CSV and MS Excel file.

For CSV

To begin, in the sources, type the following command in R in order to import a CSV file:

read_csv("Path where your Excel file is stored/File Name.csv")

?! Where is the dataset

<- is “Assigning”

df_csv <- read_csv("Path where your Excel file is stored/File Name.csv")

In the environment, there will be df_csv.

Example

Measures from the Adult Social Care Outcomes Framework - England 2021-22: Open data csv https://digital.nhs.uk/data-and-information/publications/statistical/adult-social-care-outcomes-framework-ascof/2021-22 (accessed in May 2022).

df_csv <-  read_csv("meas-from-asc-of-eng-2022-open-data-csv-v3.csv")

Example of df_csv

Import Data cont

For MS Excel

The full steps to import an MS Excel file using the readxl package.

In the sources, type the following command to install the readxl package:

install.packages("readxl")

Then, in the sources, type the following command to load the readxl package:

library(readxl)

Import an MS Excel file into R:

df_excel   <- read_excel("File Name.xlsx")

And if you want to import a specific sheet within an MS Excel file:

df_excel   <- read_excel("File Name.xlsx", sheet = "Your sheet name")

In the environment, there will be df_excel.

Example

Measures from the Adult Social Care Outcomes Framework - England 2020-21: Open data https://digital.nhs.uk/data-and-information/publications/statistical/adult-social-care-outcomes-framework-ascof/england-2020-21 (accessed in May 2022).

df_excel   <- read_excel("meas-from-asc-of-eng-2021-open-data-csv_v2.xlsx")

Example of df_excel

Data management

The objective is to combine the CSV and MS Excel into one dataframe.

Add column(s)

Add year variable:

df_csv$year <- "2021-22"
df_excel$year <- "2020-21"

Example of df_csv with year variable

Example of df_excel with year variable

Data management cont

Rename column

Standardise the column(s) name:

colnames(df_csv)[8] <- "Measure Value"

Data management cont

Standardise the column(s) strings by converting all column(s) to characters.

In the environment panel, data detail can be found at drop down of the dataframe.

Install and load tidyverse package. For more information https://www.tidyverse.org/.

install.packages("tidyverse")
library(tidyverse)
df_excel <-  df_excel %>%
              mutate(across(everything(), as.character))

Data management cont

Combine the dataframe

The bind_rows() function is to combines the rows that were present in the dataframe.

all  <- bind_rows(df_csv,df_excel)

Save or export data

Save as CSV

The write.csv() function is to create a CSV file from the data.

write.csv(the dataframe name,"file name.xlsx", row.names=FALSE)
write_csv(all, "ascof_demographics.csv", row.names=FALSE)

row.names=FALSE is export data without rows names.

Save as MS Excel

Install and load the writexl package.

install.packages("writexl")
library("writexl")

The write_xlsx() function is to create an MS Excel file from the data.

write_xlsx(the dataframe name,"path to store the Excel file/file name.xlsx")
write_xlsx(all, "ascof_demographics.xlsx")

Useful resources

Cheating Sheet

https://posit.co/blog/cheat-sheet-updates/

Examples

https://rpubs.com/FuiChing/859926

https://www.w3schools.com/r/r_examples.asp

Website

https://stackoverflow.com/

Full script

Save as CSV

# Set up a working directory
setwd("~/Powerbi Reports/ASCOF/data")


# Install packages
install.packages("readxl")
install.packages("tidyverse")
library(readxl)
library(tidyverse)


#Import data
df_csv <-  read_csv("meas-from-asc-of-eng-2022-open-data-csv-v3.csv")
df_excel <- read_excel("meas-from-asc-of-eng-2021-open-data-csv_v2.xlsx")


#Add on column
df_csv$year <- "2021-22"
df_excel$year <- "2020-21"


#Rename column 
colnames(df_csv)[8] <- "Measure Value"


#convert all columns to characters
df_excel <-  df_excel %>%
              mutate(across(everything(), as.character))


# Combine the dataframe
all  <- bind_rows(df_csv,df_excel)


# Saved data
write_csv(all, "ascof_demographics.csv", row.names=FALSE)

Save as MS Excel

# Set up a working directory
setwd("~/Powerbi Reports/ASCOF/data")


# Install packages
install.packages("readxl")
install.packages("tidyverse")
install.packages("writexl")
library(readxl)
library(tidyverse)
library(writexl)


#Import data
df_csv <-  read_csv("meas-from-asc-of-eng-2022-open-data-csv-v3.csv")
df_excel <- read_excel("meas-from-asc-of-eng-2021-open-data-csv_v2.xlsx")


#Add on column
df_csv$year <- "2021-22"
df_excel$year <- "2020-21"


#Rename column 
colnames(df_csv)[8] <- "Measure Value"


#convert all columns to characters
df_excel <-  df_excel %>%
              mutate(across(everything(), as.character))


# Combine the dataframe
all  <- bind_rows(df_csv,df_excel)


# Saved data
write_xlsx(all, "ascof_demographics.xlsx")

The End