diff --git a/data/Scripts/Express start r-script.R b/data/Scripts/Express start r-script.R new file mode 100644 index 0000000..2ef4914 --- /dev/null +++ b/data/Scripts/Express start r-script.R @@ -0,0 +1,101 @@ +## Express Start +## This R-Instat introduction is intended for professional users who have some experience with similar software + +# Dialog: Import Dataset + +# Get the current working directory +current_dir <- getwd() + +# Construct the desired file path by navigating up two directories and into "Library" +file_path <- file.path(dirname(dirname(current_dir)), "Library", "tutorials_after.RDS") + +# Read the RDS file +new_RDS <- readRDS(file = file_path) + +# Dialog: Import Dataset +data_book$import_RDS(data_RDS=new_RDS) +rm(new_RDS) +## Pivot tables in R-Instat as an excellent tool for data exploration + +# Producing a Pivot Table in R-Instat via the Pivot table dialog + +dodoma <- data_book$get_data_frame(data_name="dodoma") +last_table <- rpivotTable::rpivotTable(data=dodoma, rows=c("year","day"), cols="month_abbr", val="tmax", rendererName="Table", aggregatorName="Average") +data_book$add_object(data_name="dodoma", object_name="last_table", object_type_label="table", object_format="html", object=last_table) +data_book$get_object_data(data_name="dodoma", object_name="last_table", as_file=TRUE) +rm(list=c("last_table", "dodoma")) + + +# Producing a Pivot Table in R-Instat with the factor levels + +dodoma <- data_book$get_data_frame(data_name="dodoma") +survey_levels <- stringr::str_flatten(string=paste0("\"", levels(x=dodoma $ month_abbr), "\",")) + +dodoma <- data_book$get_data_frame(data_name="dodoma") +relevel_variables <- paste0("function(attr) { var sortAs = $.pivotUtilities.sortAs; return sortAs([", survey_levels,"]); }") +last_table <- rpivotTable::rpivotTable(data=dodoma, rows=c("year","day"), cols="month_abbr", sorters=relevel_variables, val="tmax", rendererName="Table", aggregatorName="Average") +data_book$add_object(data_name="dodoma", object_name="last_table", object_type_label="table", object_format="html", object=last_table) +data_book$get_object_data(data_name="dodoma", object_name="last_table", as_file=TRUE) +rm(list=c("last_table", "dodoma", "relevel_variables", "survey_levels")) + + +## Calculations in R-Instat using our powerful calculator dialog + +# Calculating the price per carat + +diamonds <- data_book$get_data_frame(data_name="diamonds", use_current_filter=FALSE) +attach(what=diamonds) +scalars <- data_book$get_scalars(data_name="diamonds") +attach(what=scalars) +price_c <- price / carat +data_book$add_columns_to_data(data_name="diamonds", col_name="price_c", col_data=price_c, before=FALSE, adjacent_column="carat") + +detach(name=diamonds, unload=TRUE) +detach(name=scalars, unload=TRUE) +data_book$append_to_variables_metadata(data_name="diamonds", col_names="price_c", property="labels", new_val="") +rm(list=c("price_c", "diamonds", "scalars")) + + +# Calculating the mean of the price per carat + +diamonds <- data_book$get_data_frame(data_name="diamonds", use_current_filter=FALSE) +attach(what=diamonds) +scalars <- data_book$get_scalars(data_name="diamonds") +attach(what=scalars) +mean_price <- mean(rep( price_c ), na.rm=TRUE) +mean_price +data_book$add_scalar(data_name="diamonds", scalar_name="mean_price", scalar_value=mean_price) +detach(name=diamonds, unload=TRUE) +detach(name=scalars, unload=TRUE) +rm(list=c("mean_price", "diamonds", "scalars")) + + +## Righ click and filters in R-Instat + +# Right click menu: Remove Current Filter +data_book$remove_current_filter(data_name="dodoma") + + +## Reshaping data into summaries in R-Instat + +# Using the Column Summaries to get the annual rainfalls from daily values + +data_book$calculate_summary(data_name="dodoma", columns_to_summarise="rain", factors="year", +store_results=TRUE, j=1, summaries=c("summary_count_non_missing", "summary_max", "summary_sum"), silent=TRUE) + + +## The describe menu for a graph + +# Create Filter subdialog: Created new filter +data_book$add_filter(filter=list(C0=list(column="color", operation="%in%", value=c("D","E","F"))), data_name="diamonds", filter_name="filter") + +# Data Options subdialog: Set the current filter +data_book$set_current_filter(data_name="diamonds", filter_name="filter") + +# Creating a General graph tp highlight the vast flexibility of our system + +diamonds <- data_book$get_data_frame(data_name="diamonds") +last_graph <- ggplot2::ggplot(data=diamonds, mapping=ggplot2::aes(x=clarity, y=carat, fill=clarity)) + ggplot2::geom_boxplot() + theme_grey() + ggplot2::theme(legend.position="none") + ggplot2::facet_grid(facets=cut ~ color, margins=TRUE, space="fixed") +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "diamonds")) diff --git a/data/Scripts/Prepare Menu practice r-script.R b/data/Scripts/Prepare Menu practice r-script.R new file mode 100644 index 0000000..ac29e95 --- /dev/null +++ b/data/Scripts/Prepare Menu practice r-script.R @@ -0,0 +1,185 @@ +# Prepare menu practice r-script + +# Importing the 2013 stats data of garuoua dataset to preview +# Export the data frame to an Excel file in the user's Documents directory +# Current working directory +current_dir <- getwd() + +# Construct the desired file path +file_path <- file.path(dirname(dirname(current_dir)), "Library", "Climatic","Cameroon", "garoua_1999_tr.xlsx") + +# Importing the Dodoma Dataset into R-Instat + +X2013Stats <- rio::import(file=file_path, guess_max=Inf, which=1) +data_book$import_data(data_tables=list(X2013Stats=X2013Stats)) +# Delete the 2013 stats data frame so that it can be imported again in the right format + +data_book$delete_dataframes(data_names="X2013Stats") + + +# Importing the garoua_1999_tr dataset into R-Instat with a specified number of rows to import +current_dir <- getwd() + +# Construct the desired file path +file_path <- file.path(dirname(dirname(current_dir)), "Library", "Climatic","Cameroon", "garoua_1999_tr.xlsx") + +garoua_1999_tr_list <- rio::import_list(file = file_path, guess_max = Inf, na = c("", ""), n_max = 31) +data_book$import_data(data_tables=garoua_1999_tr_list, data_names=c("X2013.Stats", "X2012.Stats", "X2011.Stats", "X2010.Stats", "X2009.Stats", "X2008.Stats", "X2007.Stats", "X2006.Sats", "X2005.Stats", "X2004.Stats", "X2003.Stats", "X2002.Stats", "X2001.Stats", "X2000.Stats", "X1999.Stats")) + +rm(garoua_1999_tr) + + +# Putting all the data sheets together by appending them to achieve a more systematic and reliable way to tidy the data + +append <- dplyr::bind_rows(data_book$get_data_frame(data_name=c("X2013.Stats","X2012.Stats","X2011.Stats","X2010.Stats","X2009.Stats","X2008.Stats","X2007.Stats","X2006.Sats","X2005.Stats","X2004.Stats","X2003.Stats","X2002.Stats","X2001.Stats","X2000.Stats","X1999.Stats")), .id="id") +data_book$import_data(data_tables=list(append=append)) + +rm(append) + + +# Transforming the text column to extract the year + +id <- data_book$get_columns_from_data(data_name="append", col_names="id", use_current_filter=FALSE) +year <- stringr::str_sub(string=id, start=2, end=5) +data_book$add_columns_to_data(data_name="append", col_name="year", col_data=year, before=FALSE, adjacent_column="id") + +rm(list=c("year", "id")) + + +# Rename the "date" column to day +data_book$rename_column_in_data(data_name="append", column_name="Date", new_val="day", label="") + + +# Stacking the months to go down rather than across + +append <- data_book$get_data_frame(data_name="append") +append_stacked <- tidyr::pivot_longer(append %>% dplyr::select(January, February, March, April, May, June, July, August, September, October, November, December, year, day), cols=c("January","February","March","April","May","June","July","August","September","October","November","December"), names_to="month", values_to="rain") +data_book$import_data(data_tables=list(append_stacked=append_stacked)) + +rm(list=c("append_stacked", "append")) + + +# Making a date variable + +date <- data_book$make_date_yearmonthday(data_name="append_stacked", day="day", month="month", year="year", year_format="%Y", month_format="%B") +data_book$add_columns_to_data(data_name="append_stacked", col_name="date", col_data=date, before=TRUE, adjacent_column="year") + +rm(date) + + +# Create Filter subdialog: Created new filter +data_book$add_filter(filter=list(C0=list(column="date", operation="is.na")), data_name="append_stacked", filter_name="filter") + + +# Filtering the date column to find missing dates + +data_book$set_current_filter(data_name="append_stacked", filter_name="filter") + + +# Right click menu: Delete Row(s) +data_book$remove_rows_in_data(data_name="append_stacked", row_names=c("338","350","362","364","366","369","371","722","734","736","738","741","743","1082","1094","1106","1108","1110","1113","1115","1454","1466","1478","1480","1482","1485","1487","1826","1838","1850","1852","1854","1857","1859","2210","2222","2224","2226","2229","2231","2570","2582","2594","2596","2598","2601","2603","2942","2954","2966","2968","2970","2973","2975","3314","3326","3338","3340","3342","3345","3347","3698","3710","3712","3714","3717","3719","4058","4070","4082","4084","4086","4089","4091","4430","4442","4454","4456","4458","4461","4463","4802","4814","4826","4828","4830","4833","4835","5186","5198","5200","5202","5205","5207","5546","5558","5570","5572","5574","5577","5579")) + + +# Right click menu: Remove Current Filter +data_book$remove_current_filter(data_name="append_stacked") + + +# Sorting the date column to make it into the right order + +data_book$sort_dataframe(data_name="append_stacked", col_names="date") + +# Right click menu: Convert Column(s) To Numeric +data_book$convert_column_to_type(data_name="append_stacked", col_names="year", to_type="numeric", ignore_labels=TRUE) + +# Right click menu: Convert Column(s) To Factor +data_book$convert_column_to_type(data_name="append_stacked", col_names="month", to_type="factor") + + +# Using the One Variable Summarise dialog to check the data + +append_stacked <- data_book$get_data_frame(data_name="append_stacked") +last_summary <- summary(data=append_stacked, object=data_book$get_columns_from_data(data_name="append_stacked", col_names=c("date","year","day","month","rain")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="append_stacked", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="append_stacked", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "append_stacked")) + + +# Generating a rainday column + +rain <- data_book$get_columns_from_data(data_name="append_stacked", col_names="rain", use_current_filter=FALSE) +rainday <- rain >= 1 +data_book$add_columns_to_data(data_name="append_stacked", col_name="rainday", col_data=rainday, before=FALSE, adjacent_column="rain") + +data_book$append_to_variables_metadata(data_name="append_stacked", col_names="rainday", property="labels", new_val="") +rm(list=c("rainday", "rain")) + + +# Using the Column Summaries dialog to make the annual rainfall summaries + +data_book$calculate_summary(data_name="append_stacked", columns_to_summarise=c("rain","rainday"), factors="year", store_results=TRUE, j=1, summaries=c("summary_count_non_missing", "summary_sum"), silent=TRUE) + + +# Using the Column Summaries dialog to make the annual rainfall summaries without the missing values + +data_book$calculate_summary(data_name="append_stacked", columns_to_summarise=c("rain","rainday"), factors="year", store_results=TRUE, na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_count_non_missing", "summary_sum"), silent=TRUE) + + +# Using the Column Summaries dialog to find the maximum daily rainfall yearly + +data_book$calculate_summary(data_name="append_stacked", columns_to_summarise="rain", factors="year", store_results=TRUE, na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_count_non_missing", "summary_max", "summary_sum"), silent=TRUE) + + +# Using the Column Summaries dialog to find the maximum daily rainfall monthly + +data_book$calculate_summary(data_name="append_stacked", columns_to_summarise="rain", store_results=TRUE, factors=c("year","month"), na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_count_non_missing", "summary_max", "summary_sum"), silent=TRUE) + + +# Using the Column Summaries dialog to find the sumof the raindays monthly + +data_book$calculate_summary(data_name="append_stacked", columns_to_summarise=c("rain","rainday"), store_results=TRUE, factors=c("year","month"), na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_sum"), silent=TRUE) + + +# Sorting the year column to organize the data by year + +data_book$sort_dataframe(data_name="append_stacked_by_year_month", col_names="year") + + +# Save the data for later use + +saveRDS(file=file.path(Sys.getenv("USERPROFILE"), "Documents", "garoua.RDS"), object=data_book) + + +# Exporting datasets as Excel files + +rio::export(x = data_book$get_data_frame(data_name = "append_stacked"),file = file.path(Sys.getenv("USERPROFILE"), "Documents", "append_stacked.xlsx")) +rio::export(x = data_book$get_data_frame(data_name = "append_stacked_by_year"),file = file.path(Sys.getenv("USERPROFILE"), "Documents", "append_stacked_by_year.xlsx")) +rio::export( x = data_book$get_data_frame(data_name="append_stacked_by_year_month"), file = file.path(Sys.getenv("USERPROFILE"), "Documents", "append_stacked_by_year_month.xlsx")) + +# Renaming the append_stacked Data Frame + +data_book$rename_dataframe(data_name="append_stacked", new_value="garoua", label="") + + +# Renaming the append_stacked_by_year Data Frame + +data_book$rename_dataframe(data_name="append_stacked_by_year", new_value="garoua_by_year", label="") + + +# Renaming the append_stacked_by_year_by_month Data Frame + +data_book$rename_dataframe(data_name="append_stacked_by_year_month", new_value="garouar_month", label="") + + +# File > Save: save file +saveRDS(file=file.path(Sys.getenv("USERPROFILE"), "Documents", "garoua.RDS"), object=data_book) + + + +# Adding a comment to make a report about the data + +data_book$add_new_comment(data_name="garoua_by_year", row="2", column="sum_rainday", comment="Missing value on February 29th in each leap year.") + + +# Adding a comment to make a report about the data + +data_book$add_new_comment(data_name="garoua_by_year", row="8", column="sum_rainday", comment="31st September was wrongly identified as having rainfall and September has 30 days") diff --git a/data/Scripts/Tutorial 1 r-script.R b/data/Scripts/Tutorial 1 r-script.R new file mode 100644 index 0000000..b1ad529 --- /dev/null +++ b/data/Scripts/Tutorial 1 r-script.R @@ -0,0 +1,113 @@ +#Tutorial 1 script version + +# Importing Data into R-Instat From the Library + +utils::data(package="ggplot2", X=diamonds) +data_book$import_data(data_tables=list(diamonds=diamonds)) + + +# Graph 1: One Variable Graph Dialog with a Combined graph + +last_graph <- data_book$graph_one_variable(data_name="diamonds", columns=c("carat","cut","color","clarity","depth","table","price","x","y","z"), numeric="geom_boxplot", categorical="geom_bar", output="combine") +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(last_graph) + + +# Graph 2: One Variable Graph Dialog with a Faceted Graph + +last_graph <- data_book$graph_one_variable(data_name="diamonds", columns=c("depth","table","price","x","y","z"), numeric="geom_boxplot", categorical="geom_bar") +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(last_graph) + + + +# Graph 3: One Variable Graph Dialog with a Faceted Graph on a Free Scale Axis + +last_graph <- data_book$graph_one_variable(data_name="diamonds", columns=c("depth","table","price","x","y","z"), numeric="geom_boxplot", categorical="geom_bar", free_scale_axis=TRUE) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(last_graph) + + +# One variable summaries for the diamonds data variables + +diamonds <- data_book$get_data_frame(data_name="diamonds") +last_summary <- summary(data=diamonds, object=data_book$get_columns_from_data(data_name="diamonds", col_names=c("carat","cut","color","clarity","depth","table","price","x","y","z")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="diamonds", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="diamonds", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "diamonds")) + + +# Graph 4: A more ambitious analysis two plus variable graph + +diamonds <- data_book$get_data_frame(data_name="diamonds") +last_graph <- GGally::ggpairs(data=diamonds, columns=c("depth","table","price")) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "diamonds")) + + +# Graph 5: A more ambitious analysis two plus variable graph with colour + +diamonds <- data_book$get_data_frame(data_name="diamonds") +last_graph <- GGally::ggpairs(data=diamonds, columns=c("depth","table","price"), ggplot2::aes(colour=cut)) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "diamonds")) + + +# Graph 6: Colour graph with numeric and factor variables + +diamonds <- data_book$get_data_frame(data_name="diamonds") +last_graph <- GGally::ggpairs(data=diamonds, columns=c("depth","table","price","color","clarity"), ggplot2::aes(colour=cut)) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "diamonds")) + + +# Graph 7: Pairwise plot: Examining the relationship between the data in the x and z variables + +x <- data_book$get_columns_from_data(data_name="diamonds", col_names="x") +z <- data_book$get_columns_from_data(data_name="diamonds", col_names="z") +last_model <- cor.test(x=x, y=z, alternative="two.sided", exact=NULL, method="pearson") +data_book$add_object(data_name="diamonds", object_name="last_model", object_type_label="model", object_format="text", object=last_model) +data_book$get_object_data(data_name="diamonds", object_name="last_model", as_file=TRUE) +diamonds <- data_book$get_data_frame(data_name="diamonds", remove_attr=TRUE) +last_graph <- GGally::ggpairs(data=diamonds, columns=c("x", "z"), lower=list(continuous=GGally::wrap('points'))) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_model", "x", "z", "last_graph", "diamonds")) + + +# Frequencies: Finding the outliers in the z variable + +last_summary <- sjmisc::frq(x=data_book$get_columns_from_data(data_name="diamonds", col_names="z", force_as_data_frame=TRUE, remove_labels=TRUE, drop_unused_filter_levels=TRUE)) +data_book$add_object(data_name="diamonds", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="diamonds", object_name="last_summary", as_file=TRUE) +rm(last_summary) + + +# Create Filter subdialog: Created new filter +data_book$add_filter(filter=list(C0=list(column="z", operation=">", value=0), C1=list(column="z", operation="<", value=10)), data_name="diamonds", filter_name="filter") + + +# Filtering the z variable to eliminate some of the outliers + +data_book$set_current_filter(data_name="diamonds", filter_name="filter") + + +# # Graph 8: Pairwise plot: Examining the relationship between the data in the x and z variables after filtering the rows to eliminate some outliers + +x <- data_book$get_columns_from_data(data_name="diamonds", col_names="x") +z <- data_book$get_columns_from_data(data_name="diamonds", col_names="z") +last_model <- cor.test(x=x, y=z, alternative="two.sided", exact=NULL, method="pearson") +data_book$add_object(data_name="diamonds", object_name="last_model", object_type_label="model", object_format="text", object=last_model) +data_book$get_object_data(data_name="diamonds", object_name="last_model", as_file=TRUE) +diamonds <- data_book$get_data_frame(data_name="diamonds", remove_attr=TRUE) +last_graph <- GGally::ggpairs(data=diamonds, columns=c("x", "z"), lower=list(continuous=GGally::wrap('points'))) +data_book$add_object(data_name="diamonds", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="diamonds", object_name="last_graph", as_file=TRUE) +rm(list=c("last_model", "x", "z", "last_graph", "diamonds")) + diff --git a/data/Scripts/Tutorial 2 r-script.R b/data/Scripts/Tutorial 2 r-script.R new file mode 100644 index 0000000..a8fe42e --- /dev/null +++ b/data/Scripts/Tutorial 2 r-script.R @@ -0,0 +1,77 @@ +# Tutorial 2 r-script + +# Importing the Dodoma Dataset into R-Instat +# Current working directory +current_dir <- getwd() + +# Construct the desired file path +file_path <- file.path(dirname(dirname(current_dir)), "Library", "Climatic", "climatic_guide_datasets.xlsx") + +# Importing the Dodoma Dataset into R-Instat + +dodoma <- rio::import(file=file_path, guess_max=Inf, which=6) +data_book$import_data(data_tables=list(dodoma=dodoma)) + +# Using the One Variable Summarise dialog to find out more about the missing values + +dodoma <- data_book$get_data_frame(data_name="dodoma") +last_summary <- summary(data=dodoma, object=data_book$get_columns_from_data(data_name="dodoma", col_names=c("year","month","day","rain","tmax","tmin","sunh")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="dodoma", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="dodoma", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "dodoma")) + + +# Create Filter subdialog: Created new filter +data_book$add_filter(filter=list(C0=list(column="year", operation=">", value=1957)), data_name="dodoma", filter_name="filter") + + +# Filter the data to start in 1958 when the temperature data starts + +data_book$set_current_filter(data_name="dodoma", filter_name="filter") + + +# Using the One Variable Summarise dialog to summarize again after reducing the number of missing values + +dodoma <- data_book$get_data_frame(data_name="dodoma") +last_summary <- summary(data=dodoma, object=data_book$get_columns_from_data(data_name="dodoma", col_names=c("year","month","day","rain","tmax","tmin","sunh")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="dodoma", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="dodoma", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "dodoma")) + + +# Summarizing the data to produce the yearly means and totals without missing values + +data_book$calculate_summary(data_name="dodoma", columns_to_summarise=c("tmax","tmin"), factors="year", store_results=TRUE, na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_count_non_missing", "summary_mean"), silent=TRUE) + + +# Summarizing the data again to produce the yearly means and totals without omitting the missing values + +data_book$calculate_summary(data_name="dodoma", columns_to_summarise=c("tmax","tmin"), factors="year", store_results=TRUE, j=1, summaries=c("summary_count_non_missing", "summary_mean"), silent=TRUE) + + +# Summarizing the data again to produce the yearly means and totals without the missing values + +data_book$calculate_summary(data_name="dodoma", columns_to_summarise=c("tmax","tmin"), factors="year", store_results=TRUE, na.rm=TRUE, na_type=c(), j=1, summaries=c("summary_count_non_missing", "summary_mean"), silent=TRUE) + + +# Producing a line plot for the mean_tmax + +dodoma_by_year <- data_book$get_data_frame(data_name="dodoma_by_year") +last_graph <- ggplot2::ggplot(data=dodoma_by_year, mapping=ggplot2::aes(y=mean_tmax, x=year)) + ggplot2::geom_line(colour="blue", size=0.8) + theme_grey() +data_book$add_object(data_name="dodoma_by_year", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="dodoma_by_year", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "dodoma_by_year")) + + +# Producing a line plot for the mean_tmin + +dodoma_by_year <- data_book$get_data_frame(data_name="dodoma_by_year") +last_graph <- ggplot2::ggplot(data=dodoma_by_year, mapping=ggplot2::aes(y=mean_tmin, x=year)) + ggplot2::geom_line(colour="blue", size=0.8) + theme_grey() +data_book$add_object(data_name="dodoma_by_year", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="dodoma_by_year", object_name="last_graph", as_file=TRUE) +rm(list=c("last_graph", "dodoma_by_year")) + + +# Saving the data for later use + +saveRDS(file=file.path(Sys.getenv("USERPROFILE"), "Documents", "dodoma.RDS"), object=data_book) diff --git a/data/Scripts/computer_skills.R b/data/Scripts/computer_skills.R new file mode 100644 index 0000000..f306293 --- /dev/null +++ b/data/Scripts/computer_skills.R @@ -0,0 +1,266 @@ +## Computer skills for R-Instat r-script practical +## Getting started in R-Instat + +# Creating a new data frame in R-Instat: Entering data + +data <- data.frame(x1=as.numeric(rep(seq(1,1000), each=1, length.out=10)), x2=as.character(rep(NA, 10))) +data_book$import_data(data_tables=list(data=data)) + +rm(data) + + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="1", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="2", new_value=6) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="3", new_value=10) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="4", new_value=14) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="5", new_value=22) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="6", new_value=16) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="7", new_value=9) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="8", new_value=4) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="9", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data", col_name="x2", rows="10", new_value=-5) + + +# Summarizing data in R-Instat using the one variable summarise dialog + +data <- data_book$get_data_frame(data_name="data") +last_summary <- summary(data=data, object=data_book$get_columns_from_data(data_name="data", col_names=c("x1","x2")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="data", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="data", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "data")) + + +# Right click menu: Converting a character column to a numeric column +data_book$convert_column_to_type(data_name="data", col_names="x2", to_type="numeric", ignore_labels=TRUE) + + +# Summarizing data in R-Instat using the one variable summarise dialog + +data <- data_book$get_data_frame(data_name="data") +last_summary <- summary(data=data, object=data_book$get_columns_from_data(data_name="data", col_names=c("x1","x2")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="data", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="data", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "data")) + + +## Making a mistake when entering your data + +# Creating a new data frame in R-Instat + +data1 <- data.frame(x1=as.numeric(rep(seq(1,1000), each=1, length.out=10)), x2=as.character(rep(NA, 10))) +data_book$import_data(data_tables=list(data1=data1)) + +rm(data1) + + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="1", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="2", new_value=6) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="3", new_value="1o") + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="4", new_value=14) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="5", new_value=22) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="6", new_value=16) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="7", new_value=9) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="8", new_value=4) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="9", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data1", col_name="x2", rows="10", new_value=-5) + +# Right click menu: Convert Column(s) To Numeric +data_book$convert_column_to_type(data_name="data1", col_names="x2", to_type="numeric", ignore_labels=TRUE) + + +# Summarizing data in R-Instat using the one variable summarise dialog when there is a missing value + +data1 <- data_book$get_data_frame(data_name="data1") +last_summary <- summary(data=data1, object=data_book$get_columns_from_data(data_name="data1", col_names=c("x1","x2")), maxsum=12, na.rm=FALSE) +data_book$add_object(data_name="data1", object_name="last_summary", object_type_label="summary", object_format="text", object=last_summary) +data_book$get_object_data(data_name="data1", object_name="last_summary", as_file=TRUE) +rm(list=c("last_summary", "data1")) + + +## Saving data in R-Instat +# Saving the data for later use to an RDS file in the user's Documents directory + +saveRDS(file=file.path(Sys.getenv("USERPROFILE"), "Documents", "computer_skills.RDS"), object=data_book) + +## Changing names in new data frames +## Entering the data with a nonnumeric value after defining the column as numeric + +# Creating a new data frame in R-Instat + +test <- data.frame(x1=as.numeric(rep(seq(1,1000), each=1, length.out=10)), max_temp=as.numeric(rep(NA, 10))) +data_book$import_data(data_tables=list(test=test)) + +rm(test) + + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="1", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="2", new_value=6) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="3", new_value=10) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="4", new_value=14) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="5", new_value=22) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="6", new_value=16) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="7", new_value=9) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="8", new_value=4) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="9", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="test", col_name="max_temp", rows="10", new_value=-5) + + +## More on the right click menu + +# Renaming Columns in R-Instat via the right-click menu + +data_book$rename_column_in_data(data_name="test", column_name="x1", new_val="row", label="") + + +# Renaming Columns in R-Instat via the right-click menu + +data_book$rename_column_in_data(data_name="test", column_name="max_temp", new_val="maxtemp", label="") + + +# Sorting data in ascending order in R-Instat via the right-click menu + +data_book$sort_dataframe(data_name="test", col_names="maxtemp") + + +# Sorting data in ascending order in R-Instat via the right-click menu + +data_book$sort_dataframe(data_name="test", col_names="row") + + +## Adding ranks in R-Instat + +# Using the transform dialog to add a rank in R-Instat + +maxtemp <- data_book$get_columns_from_data(data_name="test", col_names="maxtemp", use_current_filter=FALSE) +maxtemp1 <- rank(maxtemp) +data_book$add_columns_to_data(data_name="test", col_name="maxtemp1", col_data=maxtemp1, before=FALSE, adjacent_column="maxtemp") + +rm(list=c("maxtemp1", "maxtemp")) + +# Right click menu: Deleting columns in R-Instat +data_book$remove_columns_in_data(data_name="test", cols="maxtemp1") + + +## A big data frame in R-Instat + +# Creating a new data frame in R-Instat + +test2 <- data.frame(x1=as.numeric(rep(seq(1,1000), each=1, length.out=50000)), x2=as.character(rep(NA, 50000)), x3=as.character(rep(NA, 50000)), x4=as.character(rep(NA, 50000)), x5=as.character(rep(NA, 50000)), x6=as.character(rep(NA, 50000)), x7=as.character(rep(NA, 50000)), x8=as.character(rep(NA, 50000)), x9=as.character(rep(NA, 50000)), x10=as.character(rep(NA, 50000)), x11=as.character(rep(NA, 50000)), x12=as.character(rep(NA, 50000)), x13=as.character(rep(NA, 50000)), x14=as.character(rep(NA, 50000)), x15=as.character(rep(NA, 50000)), x16=as.character(rep(NA, 50000)), x17=as.character(rep(NA, 50000)), x18=as.character(rep(NA, 50000)), x19=as.character(rep(NA, 50000)), x20=as.character(rep(NA, 50000)), x21=as.character(rep(NA, 50000)), x22=as.character(rep(NA, 50000)), x23=as.character(rep(NA, 50000)), x24=as.character(rep(NA, 50000)), x25=as.character(rep(NA, 50000)), x26=as.character(rep(NA, 50000)), x27=as.character(rep(NA, 50000)), x28=as.character(rep(NA, 50000)), x29=as.character(rep(NA, 50000)), x30=as.character(rep(NA, 50000)), x31=as.character(rep(NA, 50000)), x32=as.character(rep(NA, 50000)), x33=as.character(rep(NA, 50000)), x34=as.character(rep(NA, 50000)), x35=as.character(rep(NA, 50000)), x36=as.character(rep(NA, 50000)), x37=as.character(rep(NA, 50000)), x38=as.character(rep(NA, 50000)), x39=as.character(rep(NA, 50000)), x40=as.character(rep(NA, 50000)), x41=as.character(rep(NA, 50000)), x42=as.character(rep(NA, 50000)), x43=as.character(rep(NA, 50000)), x44=as.character(rep(NA, 50000)), x45=as.character(rep(NA, 50000)), x46=as.character(rep(NA, 50000)), x47=as.character(rep(NA, 50000)), x48=as.character(rep(NA, 50000)), x49=as.character(rep(NA, 50000)), x50=as.character(rep(NA, 50000)), x51=as.character(rep(NA, 50000)), x52=as.character(rep(NA, 50000)), x53=as.character(rep(NA, 50000)), x54=as.character(rep(NA, 50000)), x55=as.character(rep(NA, 50000)), x56=as.character(rep(NA, 50000)), x57=as.character(rep(NA, 50000)), x58=as.character(rep(NA, 50000)), x59=as.character(rep(NA, 50000)), x60=as.character(rep(NA, 50000)), x61=as.character(rep(NA, 50000)), x62=as.character(rep(NA, 50000)), x63=as.character(rep(NA, 50000)), x64=as.character(rep(NA, 50000)), x65=as.character(rep(NA, 50000)), x66=as.character(rep(NA, 50000)), x67=as.character(rep(NA, 50000)), x68=as.character(rep(NA, 50000)), x69=as.character(rep(NA, 50000)), x70=as.character(rep(NA, 50000)), x71=as.character(rep(NA, 50000)), x72=as.character(rep(NA, 50000)), x73=as.character(rep(NA, 50000)), x74=as.character(rep(NA, 50000)), x75=as.character(rep(NA, 50000)), x76=as.character(rep(NA, 50000)), x77=as.character(rep(NA, 50000)), x78=as.character(rep(NA, 50000)), x79=as.character(rep(NA, 50000)), x80=as.character(rep(NA, 50000)), x81=as.character(rep(NA, 50000)), x82=as.character(rep(NA, 50000)), x83=as.character(rep(NA, 50000)), x84=as.character(rep(NA, 50000)), x85=as.character(rep(NA, 50000)), x86=as.character(rep(NA, 50000)), x87=as.character(rep(NA, 50000)), x88=as.character(rep(NA, 50000)), x89=as.character(rep(NA, 50000)), x90=as.character(rep(NA, 50000)), x91=as.character(rep(NA, 50000)), x92=as.character(rep(NA, 50000)), x93=as.character(rep(NA, 50000)), x94=as.character(rep(NA, 50000)), x95=as.character(rep(NA, 50000)), x96=as.character(rep(NA, 50000)), x97=as.character(rep(NA, 50000)), x98=as.character(rep(NA, 50000)), x99=as.character(rep(NA, 50000)), x100=as.character(rep(NA, 50000)), x101=as.character(rep(NA, 50000)), x102=as.character(rep(NA, 50000)), x103=as.character(rep(NA, 50000)), x104=as.character(rep(NA, 50000)), x105=as.character(rep(NA, 50000)), x106=as.character(rep(NA, 50000)), x107=as.character(rep(NA, 50000)), x108=as.character(rep(NA, 50000)), x109=as.character(rep(NA, 50000)), x110=as.character(rep(NA, 50000)), x111=as.character(rep(NA, 50000)), x112=as.character(rep(NA, 50000)), x113=as.character(rep(NA, 50000)), x114=as.character(rep(NA, 50000)), x115=as.character(rep(NA, 50000)), x116=as.character(rep(NA, 50000)), x117=as.character(rep(NA, 50000)), x118=as.character(rep(NA, 50000)), x119=as.character(rep(NA, 50000)), x120=as.character(rep(NA, 50000)), x121=as.character(rep(NA, 50000)), x122=as.character(rep(NA, 50000)), x123=as.character(rep(NA, 50000)), x124=as.character(rep(NA, 50000)), x125=as.character(rep(NA, 50000)), x126=as.character(rep(NA, 50000)), x127=as.character(rep(NA, 50000)), x128=as.character(rep(NA, 50000)), x129=as.character(rep(NA, 50000)), x130=as.character(rep(NA, 50000)), x131=as.character(rep(NA, 50000)), x132=as.character(rep(NA, 50000)), x133=as.character(rep(NA, 50000)), x134=as.character(rep(NA, 50000)), x135=as.character(rep(NA, 50000)), x136=as.character(rep(NA, 50000)), x137=as.character(rep(NA, 50000)), x138=as.character(rep(NA, 50000)), x139=as.character(rep(NA, 50000)), x140=as.character(rep(NA, 50000)), x141=as.character(rep(NA, 50000)), x142=as.character(rep(NA, 50000)), x143=as.character(rep(NA, 50000)), x144=as.character(rep(NA, 50000)), x145=as.character(rep(NA, 50000)), x146=as.character(rep(NA, 50000)), x147=as.character(rep(NA, 50000)), x148=as.character(rep(NA, 50000)), x149=as.character(rep(NA, 50000)), x150=as.character(rep(NA, 50000)), x151=as.character(rep(NA, 50000)), x152=as.character(rep(NA, 50000)), x153=as.character(rep(NA, 50000)), x154=as.character(rep(NA, 50000)), x155=as.character(rep(NA, 50000)), x156=as.character(rep(NA, 50000)), x157=as.character(rep(NA, 50000)), x158=as.character(rep(NA, 50000)), x159=as.character(rep(NA, 50000)), x160=as.character(rep(NA, 50000)), x161=as.character(rep(NA, 50000)), x162=as.character(rep(NA, 50000)), x163=as.character(rep(NA, 50000)), x164=as.character(rep(NA, 50000)), x165=as.character(rep(NA, 50000)), x166=as.character(rep(NA, 50000)), x167=as.character(rep(NA, 50000)), x168=as.character(rep(NA, 50000)), x169=as.character(rep(NA, 50000)), x170=as.character(rep(NA, 50000)), x171=as.character(rep(NA, 50000)), x172=as.character(rep(NA, 50000)), x173=as.character(rep(NA, 50000)), x174=as.character(rep(NA, 50000)), x175=as.character(rep(NA, 50000)), x176=as.character(rep(NA, 50000)), x177=as.character(rep(NA, 50000)), x178=as.character(rep(NA, 50000)), x179=as.character(rep(NA, 50000)), x180=as.character(rep(NA, 50000)), x181=as.character(rep(NA, 50000)), x182=as.character(rep(NA, 50000)), x183=as.character(rep(NA, 50000)), x184=as.character(rep(NA, 50000)), x185=as.character(rep(NA, 50000)), x186=as.character(rep(NA, 50000)), x187=as.character(rep(NA, 50000)), x188=as.character(rep(NA, 50000)), x189=as.character(rep(NA, 50000)), x190=as.character(rep(NA, 50000)), x191=as.character(rep(NA, 50000)), x192=as.character(rep(NA, 50000)), x193=as.character(rep(NA, 50000)), x194=as.character(rep(NA, 50000)), x195=as.character(rep(NA, 50000)), x196=as.character(rep(NA, 50000)), x197=as.character(rep(NA, 50000)), x198=as.character(rep(NA, 50000)), x199=as.character(rep(NA, 50000)), x200=as.character(rep(NA, 50000))) +data_book$import_data(data_tables=list(test2=test2)) + +rm(test2) + +# Deleting data frames in R-Instat using the R-Instat + +data_book$delete_dataframes(data_names="test2") + + +## The tool bar + +# Creating a new data frame in R-Instat + +data2 <- data.frame(x1=as.numeric(rep(NA, 5)), x2=as.numeric(rep(NA, 5))) +data_book$import_data(data_tables=list(data2=data2)) + +rm(data2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x1", rows="1", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x2", rows="1", new_value=16) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x1", rows="2", new_value=6) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x2", rows="2", new_value=9) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x1", rows="3", new_value=10) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x2", rows="3", new_value=4) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x1", rows="4", new_value=14) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x2", rows="4", new_value=2) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x1", rows="5", new_value=22) + +# Replace Value In Data: Entering data in R-Instat +data_book$replace_value_in_data(data_name="data2", col_name="x2", rows="5", new_value=-5) + + +# Getting graphical summary of the data using the one variable graph dialog + +last_graph <- data_book$graph_one_variable(data_name="data2", columns=c("x1","x2"), numeric="geom_boxplot", categorical="geom_bar") +data_book$add_object(data_name="data2", object_name="last_graph", object_type_label="graph", object_format="image", object=check_graph(graph_object=last_graph)) +data_book$get_object_data(data_name="data2", object_name="last_graph", as_file=TRUE) +rm(last_graph) + +# Viewing the one variable graph displayed by plotly +view_object_data(object=plotly::ggplotly(p=data_book$get_last_object_data(object_type_label="graph", as_file=FALSE)), object_format="html") + + +