Wednesday, May 10, 2023

R Tutorial Playlist - SQLite database

Process of connecting to SQLite database and executing commands and readings goes like this.

library(RSQLite)

db <- dbConnect(RSQLite::SQLite, "path for db file")

dbExecute(db, "SQL command")

response <- dbGetQuery(db, "SQL command")

dbDisconnect(db)

In first line, we are importing RSQLite package. In second line db connection structure is created using dbConnect function where we are going to provide two arguments; first argument is RSQLite::SQLite and second is path to database file. In third line is example of how command execution works; first argument is database structure and second is actual database query. In fourth line is example of query that should return database readings; here is the same case in regards of arguments; first argument is database connection structure, and second line is sql select statement. Purpose of final line is to close database connection where database structure is provided as the only argument.


For video tutorial, check the link below.



R Tutorial Playlist - JSON

 For purpose of reading of json file, there are two most popular packages; rjson and jsonlite.

Code for jsonlite looks like:

library("jsonlite")

j <- read_json("path to json file")

j <- as.data.frame(j)

In first line, we are importing actual library. In second line with function read_json() we are going to provide argument of path to json file we want to read, and in third, received structure is converted into data frame.


Code for rjson looks like:

library("rjson")

j <- fromJSON("path to json file")

j <- as.data.frame(j)

Same as in previous case, in first line, library is imported. Function fromJSON, works in the same way as function read_json in previous case, and finally, received structure is going to be converted to data frame as well.


For video tutorial, check the link below.



Friday, May 5, 2023

R Tutorial Playlist - XML

XML is very useful format for the purpose of data transfer. In here we are going to deal with two packages for xml manipulation; XML and xml2.


In case of xml2 library, first we have to import library itself.

 library("xml2")

then in order to read content of file with path a into xml structure, we have to use function read_xml

xml <- read_xml(a)

now, in order to get list of all xml children, use function xml_children like in case:

x <- xml_children(xml)

If you want to access elements of particular child from your xml structure, do it in this way

first <- xml_children(xml[1])

Finally with function of xml_text, you have to provide argument of xml child you want to get value from.

text <- xml_text(first[1])

And if attribute of that child is about, this is the way.

attr <- xml_attr(x[1], b)

Where b is path to file with xml content.


And there is second interesting library and that is xml

library("xml")

xml <- xmlToDataFrame(a)

where a is path to file with xml content.


For video tutorial, check link below.




R Tutorial Playlist - Please select a CRAN mirror for use in this session

 Sometimes, when you want to install new packages in R, you R session will have no default repository selected. In order to solve that problem, as second argument to install.packages function, you will have to provide optional argument repos, like in case:

install.packages("openxlsx", repos="http://cran.us.r-project.org")

Mirror http://cran.us.r-project.org is active at date of this post writing.

For video tutorial, check the link below.



Thursday, May 4, 2023

R Tutorial Playlist - CSV documents

 CSV or coma separated values is format suited both for transfer and data storage.

1,2,3

4,5,6

7,8,9

This is basic form of csv document. Columns separated by coma and row of data per row of text.

There are only two function you have to worry about when dealing with csv from R programming language.

  • write.csv(a, b) where a is data.frame that should be saved to the file, and b is path to the file.
  • a<- read.csv(b) where a is data.frame where we are going to write read data from the file, and b is path to the file.

For video tutorial, check the link below.


Tuesday, May 2, 2023

R Tutorial Playlist - How to read and write simple textual content to a file

 If you want to print some text to a file, you can use function cat for that purpose

cat("text to be printed to the file", "./the file.txt")

For reading back of that content to R session, you can use function read.delim like

content <- read.delim("./the file.txt", header= FALSE)

But keep in mind that read.delim is designed for reading of table data, so in case of some regular text, text is going to be returned in the form of list.


For video tutorial, check the link below.



R Tutorial Playlist - By value, by reference

 In this tutorial, we are introducing one new package- lobstr that contains functions for memory management and in here we are dealing with three of its functions.

  • obj_size that is going to return size in bytes for variable provided as argument.
  • mem_used is going to return memory used by the session in bytes.
  • obj_addr is going to return hexadecimal representation of memory address of variable provided as argument.
First thing that you must know about variables in regards of memory is that whenever value of some variable is changed, R creates new address for that variable. If we provide one variable as value for another variable like in case
a <- 10
b <- a
not only that a and b will be of equal value but of equal address too. Only way to avoid that is to provide value as part of collection like
a <- c( b)
now a will hold all of elements of b but not its address; a and b will be two completely different variables. Same stands for function arguments. If you provide a to a function as an argument, that argument is going to be treated as passed by reference; else if you provide c( a), in this case it will be treated as passed by value.

Video tutorial link below.

 

Monday, May 1, 2023

R Tutorial Playlist - Inline coding

Inline coding is one more way R developer could write R code. If you write for loop or function like

a <- function(b){

    print(b)

}

this function could be coded in another way

a <- function(b)

print(b)

So, without any brackets. And finally, you can do something like

a <- function(b) print(b)

All three ways are absolutely fine. With second and third way however, you have to take into account that you can't provide more than one statement to function or loop.


For video tutorial, check the link below.



R Tutorial Playlist - Inf NaN and NA

We have three kinds of values in R that could cause us a problem if we are not careful.
  • NA stands for not available; usually happens when we don't have value present in our column.
  • NaN stands for not a number; value that can't be used in calculations.
  • Inf stands for infinity and we can get it if we divide some number with 0.

With is.finite(), we can check is some value finite and function is.infinite() does it in reversed way.

With function is.nan() we can check is our value NaN or not.

With function is.na() we can check is our value na or not; if we want to ignore na value, we can use function na.omit() and if we want to replace all of na values from single column with 0, we can do it in this way.

data$column[is.na(data$column)] <- 0


For video tutorial, check the link below.