Lam Fui Ching
15-5-2023
Log a ticket with ICT Support Portal to download and install the following:-
Four RStudio Panels
To view:
To view:
Two options:-
Option 1: Script
Option 2:
Firstly, move the cursor anywhere in the start of the line, then:-
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 a CSV and MS Excel file.
To begin, in the sources, type the following command in R in order to import a CSV file:
<- is “Assigning”
In the environment, there will be df_csv.
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).
Example of df_csv
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:
Then, in the sources, type the following command to load the readxl package:
Import an MS Excel file into R:
And if you want to import a specific sheet within an MS Excel file:
In the environment, there will be df_excel.
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).
Example of df_excel
The objective is to combine the CSV and MS Excel into one dataframe.
Add year variable:
Example of df_csv with year variable
Example of df_excel with year variable
Standardise the column(s) name:
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/.
The bind_rows() function is to combines the rows that
were present in the dataframe.
The write.csv() function is to create a CSV file from
the data.
row.names=FALSE is export data without rows names.
Install and load the writexl package.
The write_xlsx() function is to create an MS Excel file
from the data.
https://posit.co/blog/cheat-sheet-updates/
https://rpubs.com/FuiChing/859926
https://www.w3schools.com/r/r_examples.asp
# 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)# 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")