Wednesday, June 24, 2020

Fast and Dingy Refresher for R Programming


I have been going over R programming lately, and find that having a list of questions and answers covering various topics useful for review. The target audience of this post is someone comfortable with programming in general and might even have experience with the language but needs a refresher because it has been years since they last use it. In other words, this is my personal note sheet for R.  

As of June 24, 2020, this is version 0.01a of my very incomplete note sheet. I hope to add to this over time. The current version references the following websites: https://www.datamentor.io/r-programming/while-loop/, https://www.datacamp.com/community/tutorials/r-data-import-tutorial and https://www.w3resource.com/r-programming-exercises/basic/index.php. Do pay them a visit and see if you prefer the reference sites over this. You can try out the R code below with an online interpreter such as https://trinket.io/embed/R (use ctrl+enter to run the code or use the run button).

Input/Output


#Reading in strings and numerics and printing out the variables:
name = readline(prompt="Input your name: ")
age =  readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
print(R.version.string)
#Reading a csv file
# movie_data = read.csv(file="movies.csv", header=TRUE, sep=",")
print("Read a csv file directly from the web.")
print("Note. You can find a csv file online by googling for site:edu filetype:csv.")

df1 = read.table("http://www.calvin.edu/~stob/courses/m243/S13/Superbowl.csv",
                 header = TRUE,
                 sep = ",")
df1

df2 = read.csv("http://www.calvin.edu/~stob/courses/m243/S13/Superbowl.csv",
                 header = TRUE)
df2


#Reading a json file (and importing the rjson library)

print("Note. I tested that import the library rjson works for trinket.io's R Intrepretor. Lots of other libraries doesn't work with trinket.io unfortunately.")
install.packages("rjson",repos = "https://cran.r-project.org")
library("rjson")
print("Yes. I trade on Bittrex -- see the sidebar for referral -- and download data via their API for analysis.")
JsonData <- fromJSON(file="https://api.bittrex.com/v3/markets/ETH-USDT/summary" )
print(JsonData)


Information


#Listing content of workspace/memory
name = "Python";
n1 =  10;
nums = c(10, 20, 30, 40, 50, 60)
print(ls())
print("Details of the objects in memory:")
print(ls.str())
# Get current (system) date/time

print("System's idea of the current date with and without time:")
print(Sys.Date())
print(Sys.time())

Data analysis

#Random number generation (for Monte Carlos)
print("Random number sampling/generation.")
v = sample(-50:50, 10, replace=TRUE)
print("Note the with replacement option.")
print("10 random integer values between -50 and +50:")
print(v)
print("Random normal distribution sample.")
n = floor(rnorm(1000, 50, 100))
print(paste("Mean: ",mean(n)))
print(paste("Stdev:",sqrt(var(n))))
#Tabulation of data (Frequency)
n = c(10,10,10,20,30,30)
print('List of random numbers in normal distribution:')
print(n)
t = table(n)
print(t)

#Plotting

n = floor(rnorm(200, 0, 1))

print("Bar plot.")
barplot(n,
main = "Some plot",
xlab = "XData",
ylab = "YData",
col = "darkred",
horiz = FALSE)

print("Bar plot of histogram.")
t = table(n)
barplot(t)

Data Structure


#Creating vectors of different element types
print("Creating vectors of diffrent element types")
a = c(1, 2, 5, 3, 4, 0, -1, -3)
b = c("Red", "Green", "White")
c = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
print(a)
print(typeof(a))
print(b)
print(typeof(b))
print(c)
print(typeof(c))
#Various ways to combine items in a list
print("Various ways to combine items in a list:")
a<-c(1,2,3)
print(a)
b<-c(4,5,6)
print(b)
d1<-c(1,2,3,c(4,5,6))
print(d1)
d2<-cbind(a,b)
print(d2)
d3<-rbind(a,b)
print(d3)
#Creating matrices of different dimensions
m1 = matrix(1:20, nrow=5, ncol=4)
print("5 × 4 matrix:")
print(m1)
print("Vectorize:"
print(as.vector(m1))
cells = c(1,3,5,7,8,9,11,12,14)rnames = c("Row1", "Row2", "Row3")
cnames = c("Col1", "Col2", "Col3")
m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames))
print("3 × 3 matrix with labels, filled by rows: ")
print(m2)

# Creating a list of mixed elements
print("Creating a list of mixed elements.")
l = list(
     c(1, 2, 2, 5, 7, 12), 
     month.abb,
     matrix(c(3, -8, 1, -3), nrow = 2),
     asin
)
print("Content of the list:")
print(l)
# Creating a dataframe

Employees = data.frame(Name=c("Anastasia S",
"Dima R","Katherine S","JAMES A",
"LAURA MARTIN"),
Gender=c("M","M","F","F","M"),
Age=c(23,22,25,26,32),
Designation=c("Clerk","Manager","Exective",
"CEO","ASSISTANT"),
SSN=c("123-34-2346","123-44-779",
"556-24-433","123-98-987","679-77-576")
)
print("Details of the employees:")                     
print(Employees)




String Manipulation

print("First 10 letters in lower case:")
print("Note letters and LETTERS are system variables.")
t = head(letters, 10)
print(t)
print("Last 10 letters in upper case:")
t = tail(LETTERS, 10)
print(t)
print("Letters between 22nd to 24th letters in upper case:")
e = tail(LETTERS[22:24])
print(e)
print("Convert to lower case letters :")
print("Note toupper converts to upper case.")
e = tolower(tail(LETTERS[22:24]))
print(e)

Program Flow


#Numeration for loops (and calculations)
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Sequence of numbers from 20 to 50 and 10 to 30:")
print(c(seq(20,50),seq(10,30)))
print("Sequence of unique numbers from 20 to 50 and 10 to 30:")
print(unique(c(seq(20,50),seq(10,30))))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))


#IF/ELSE and FORloops
print("Showing off IF and FOR loop syntax.")
n = 3
if (n >= 2) {
     x = seq(2, n)
     prime_nums = c()
     for (i in seq(2, n)) {
          if (any(x == i)) {
               prime_nums = c(prime_nums, i)
               x = c(x[(x %% i) != 0], i)
         }
     }
} else {
     stop("Input number should be at least 2.")
}
print(prime_nums)


#WHILE loop

i = 1
while (i < 20) {
     print(i)
     i = i+1
}

Miscelleanous


#Function definition
hello_world <- function(n) {
     print(paste("This is the number",n))
}
print("Run program after definition.")
hello_world(12)