Code Is Poetry

Life is Binary

Programming Language - R

Lists - Part-6

# Vector with numerics from 1 up to 10my_vector <- 1:10 # Matrix with numerics from 1 up to 9my_matrix <- matrix(1:9, ncol = 3)# First 10 elements of the built-in data frame mtcarsmy_df <- mtcars[1:10,]# Construct list with these different elements:my_list <- list(my_vector,my_matrix,my_df) ################################## # Vector with numerics from 1 up to 10my_vector <- 1:10 # Matrix with numerics from 1 up to 9my_matrix <- matrix(1:9, ncol = 3)# First 10 elements of...

Read more

data frame - Part-5

# Print out built-in R data frame(mtcars ) ############################### # Call head() on mtcarshead(mtcars)################################ # Investigate the structure of mtcarsstr(mtcars) ##################################### # Definition of vectorsname <- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")type <- c("Terrestrial planet", "Terrestrial planet", "Terrestrial planet",           "Terrestrial...

Read more

Factors - Part-4

# Assign to the variable theory what this chapter is about!theory <- "factors for categorical variables" ######################### # Gender vectorgender_vector <- c("Male", "Female", "Female", "Male", "Male")# Convert gender_vector to a factorfactor_gender_vector <- factor(gender_vector)  # Print out factor_gender_vector(factor_gender_vector) ################################## # Animalsanimals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse")factor_animals_vector...

Read more

Matrix - Part-3

# Construct a matrix with 3 rows that contain the numbers 1 up to 9(matrix(1:9,byrow=TRUE,nrow=3)) ################################# # Box office Star Wars (in millions!)new_hope <- c(460.998, 314.4)empire_strikes <- c(290.475, 247.900)return_jedi <- c(309.306, 165.8)# Create box_officebox_office <- c(new_hope, empire_strikes, return_jedi)# Construct star_wars_matrixstar_wars_matrix <- matrix(box_office,byrow = TRUE,nrow=3) ########################################## # Box...

Read more

Vectors in R - Part-2

# Define the variable vegasvegas <- "Go!"###################### numeric_vector <- c(1, 10, 49)character_vector <- c("a", "b", "c")# Complete the code for boolean_vectorboolean_vector <- c(TRUE, FALSE, TRUE) ############################# # Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10) ############################### # Poker winnings from Monday to...

Read more

Basic in R - Part-1

# Calculate 3 + 43 + 4# Calculate 6 + 126 + 12####################### # An addition5 + 5 # A subtraction5 - 5 # A multiplication3 * 5 # A division(5 + 5) / 2 # Exponentiation2^5# Modulo28 %% 6 ################################ # Assign the value 42 to xx <- 42# Print out the value of the variable xx ##################################### Assign the value 5 to the variable my_applesmy_apples <- 5# Print out the value of the variable...

Read more

Condition and loop in R

# if (condition) expr1 else expr2x <- 2; y <- 10if (x==0) y <- 0 else y <- y/x #y# for (variable in sequence) exprx <- 3ASum <- 0; AProd <- 1for (i in 1:x){  ASum <- ASum + i  AProd <- AProd * i}ASum # equivalent to sum(1:x)AProd # equivalemt to prod(1:x)

Read more

factors are used to store categorical data

#factors are used to store categorical dataeyeColor.v <- c("blue","green","brown","yellow","blue","blue","blue")eyeColor.f <- factor(c("blue","green","brown","yellow","blue","blue","blue")) # 1 2 3 4eyeColor.f # it will be displayed as different levels of the factoras.numeric(eyeColor.f)

Read more

Vectors in R

#vectors v1 <- c(1,5,11,33)v2 <- c(2,5,11,13)v1+v2v <- rep(0,10)v <- rep(1:10, times=2)vv <- rep(1:10, each=2)vv <- seq(10,20,2)v1 <- 1:5v2 <- 2:10v2 <- c(2,3,4,5,6,7,8)v2v2[2:4]v2[6:9]sd(v2)sum(v2)# summation calculations can be applied over entire vectorsum(v1)

Read more

Matrix creation and selection

# creating matrix(m <- matrix(data=1, nrow = 5, ncol = 4))(m <- matrix(1,3,4))#[,1] [,2] [,3] [,4]#[1,]    1    1    1    1#[2,]    1    1    1    1#[3,]    1    1    1    1#[4,]    1    1    1    1#[5,]    1   ...

Read more

R tutorial 1 - igraph

#loading igraph for network analysisrequire(igraph)# creting trivial graphg <- graph(c(1,2, 1,3, 2,3, 3,5),n=5)plot(g)# create random graph with 40 nodes each with 4 childreng <- graph.tree(40,4)plot(g)#diff layout for graph#circleplot(g, layout=layout.circle)#fruchterman.reingoldplot(g, layout=layout.fruchterman.reingold)#graphoptplot(g, layout=layout.graphopt)# interactive graph (graph node can me moved around with mouse select & drag)tkplot(g, layout=layout.graphopt)#3d graph...

Read more