Chapter 10
To write more sophisticated programs with R, you’ll need to control the flow and order of execution in your code. One fundamental way to do this is to make the execution of certain sections of code dependent on a condition. Another basic control mechanism is the loop, which repeats a block of code a certain number of times. In this chapter, we’ll explore these core programming techniques using if-else statements, for and while loops, and other control structures.
10.1 If statements The if statement is the key to controlling exactly which operations are carried out in a given chunk of code. An if statement runs a block of code only if a certain condition is true. These constructs allow a program to respond differently depending on whether a condition is TRUE or FALSE.
10.1.1 Stand-Alone Statement
Let’s start with the stand-alone if statement, which looks something like this:
Maku <- 2
Destiny <- -5
if(#condition
Maku>Destiny){
Maku*-10
# Result
}
## [1] -20
#Note the differences
Maku <- 2
Destiny <- -5
if(#condition
Maku<Destiny){ #The code to be executed
Maku*-10
#No result
}
The condition is placed in parentheses after the if keyword. This condition must be an expression that yields a single logical value (TRUE or FALSE). If it’s TRUE, the code in the braces, {}, will be executed. If the condition isn’t satisfied, the code in the braces is skipped, and R does nothing (or continues on to execute any code after the closing brace).
Here’s a simple example. In the console, store the following:
b <- 3
mynumber <- 4
#Now, in the R editor, write the following code chunk:
if(b<=mynumber){
b^2
}
## [1] 9
To illustrate a more complicated if statement, consider the following two new objects:
myvec <- c(2.73,5.40,2.15,5.29,1.36,2.16,1.41,6.97,7.99,9.52)
myvec
## [1] 2.73 5.40 2.15 5.29 1.36 2.16 1.41 6.97 7.99 9.52
mymat <- matrix(c(2,0,1,2,3,0,3,0,1,1),5,2)
mymat
## [,1] [,2]
## [1,] 2 0
## [2,] 0 3
## [3,] 1 0
## [4,] 2 1
## [5,] 3 1
#Use these two objects in the code chunk given here:
if(any((myvec-1)>9)||matrix(myvec,2,5)[2,1]<=6){
cat("Condition satisfied --\n")
new.myvec <- myvec
new.myvec[seq(1,9,2)] <- NA
mylist <- list(aa=new.myvec,bb=mymat+0.5)
cat("-- a list with",length(mylist),"members now exists.")
}
## Condition satisfied --
## -- a list with 2 members now exists.
#Examining object mylist
Visualization: Friday 24th Oct,2025.
library(tidyverse)
library(palmerpenguins)
library(ggthemes)
#Load Data
data("penguins")
data <- penguins
data
## # A tibble: 344 × 8
## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <fct> <fct> <dbl> <dbl> <int> <int>
## 1 Adelie Torgersen 39.1 18.7 181 3750
## 2 Adelie Torgersen 39.5 17.4 186 3800
## 3 Adelie Torgersen 40.3 18 195 3250
## 4 Adelie Torgersen NA NA NA NA
## 5 Adelie Torgersen 36.7 19.3 193 3450
## 6 Adelie Torgersen 39.3 20.6 190 3650
## 7 Adelie Torgersen 38.9 17.8 181 3625
## 8 Adelie Torgersen 39.2 19.6 195 4675
## 9 Adelie Torgersen 34.1 18.1 193 3475
## 10 Adelie Torgersen 42 20.2 190 4250
## # ℹ 334 more rows
## # ℹ 2 more variables: sex <fct>, year <int>
penguins
## # A tibble: 344 × 8
## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <fct> <fct> <dbl> <dbl> <int> <int>
## 1 Adelie Torgersen 39.1 18.7 181 3750
## 2 Adelie Torgersen 39.5 17.4 186 3800
## 3 Adelie Torgersen 40.3 18 195 3250
## 4 Adelie Torgersen NA NA NA NA
## 5 Adelie Torgersen 36.7 19.3 193 3450
## 6 Adelie Torgersen 39.3 20.6 190 3650
## 7 Adelie Torgersen 38.9 17.8 181 3625
## 8 Adelie Torgersen 39.2 19.6 195 4675
## 9 Adelie Torgersen 34.1 18.1 193 3475
## 10 Adelie Torgersen 42 20.2 190 4250
## # ℹ 334 more rows
## # ℹ 2 more variables: sex <fct>, year <int>
# 1.2- Making a ggplot
ggplot(data) #Make the ggplot object-this should be blank.
#Now, how will the variables be mapped onto the plot?
#This mapping the variables creates automatic axes
# Rooting the target variables into the coordinates.
ggplot(data,
mapping=aes(flipper_length_mm, #Plot object in mapping
body_mass_g))
#Note that the geom object is different from the plot object.
#Here the geom objects are the PENGUINS and the plot objects are: flipper_length_mm and body_mass_g!
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point() # Geom objects in point.
#Lets add another target variable, but this time as the fill of the color. Observe that target variables are mapped.
ggplot(data,
mapping = aes(flipper_length_mm,
body_mass_g,color = species))+
geom_point()
#Lets throw on a smoothed line
ggplot(data,
mapping = aes(flipper_length_mm,
body_mass_g,
color = species))+
geom_point()+
geom_smooth(method = 'lm') #method is a linear model, body_mass_g ~ flipper_length_mm by color.
#Note: arguments specified in plot object are passed down to geom_point/smooth and are then done locally. To solve the problem of different line of smooth; we pass color to geom_point.
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point(mapping = aes(color = species))+
geom_smooth(method = 'lm')
#But sometimes people see color differently.Lets make sure our species is identified two ways.
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point(mapping = aes(color = species, shape = species))+ # setting every point object (penguins) to a specific shape and color.
geom_smooth(method = 'lm')
#Now maybe we want to adjust our labels a little bit
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point(mapping = aes(colour = species,shape = species))+
geom_smooth(method = 'lm')+
labs(
title = 'Body Mass vs. Flipper Length',
subtitle = 'Sorted by Species',
x = 'Flipper Length(mm)', y = 'Body Mass (g)',
color = 'Species', shape = 'Species'
)+
scale_color_colorblind()
#Improving my knowledge of Visualization on 31st Oct, 2025.
#1.2 Exercises
#With Nigeria Agricultural data (cleaned) Data: Visualization Data
Nig_data1 <- "C:/Users/hp/Downloads/nigeria_agricultural_economic_indicators_1950_2023.csv" %>% read.csv()
# Cleaning and preparing the data
Clean_Ng1 <- na.omit(Nig_data1)
Clean_Ng1
## year avg_temp_c precipitation_mm rel_humidity crop_prod_index
## 32 1981 26.40 1128.72 58.43 21.81
## 33 1982 26.56 1073.85 59.40 22.23
## 34 1983 26.85 866.06 54.37 22.16
## 35 1984 26.82 1037.82 57.04 23.67
## 36 1985 26.59 1188.69 57.96 25.09
## 37 1986 26.76 1091.10 58.43 27.49
## 38 1987 27.36 1060.67 55.33 28.16
## 39 1988 26.80 1292.39 58.47 32.81
## 40 1989 26.24 1087.29 55.82 36.78
## 41 1990 27.02 1048.21 57.27 38.98
## 42 1991 26.68 1181.01 60.11 44.86
## 43 1992 26.24 1105.24 58.25 48.45
## 44 1993 26.81 1040.79 57.70 50.95
## 45 1994 26.48 1292.50 59.26 52.37
## 46 1995 26.71 1168.81 58.48 54.75
## 47 1996 26.81 1101.24 58.40 57.45
## 48 1997 26.80 1078.70 59.21 59.36
## 49 1998 27.39 1097.34 56.79 61.10
## 50 1999 26.88 1241.89 58.99 63.76
## 51 2000 26.66 1033.15 56.13 64.38
## 52 2001 26.73 998.16 56.64 63.49
## 53 2002 26.86 1051.55 57.70 66.64
## 54 2003 27.07 1007.59 58.43 70.19
## 55 2004 27.14 999.91 57.47 74.89
## 56 2005 27.50 988.48 57.50 79.41
## 57 2006 27.45 1008.70 56.66 85.08
## 58 2007 27.00 1040.05 57.01 78.37
## 59 2008 26.82 1087.07 56.78 83.65
## 60 2009 27.50 1037.43 58.28 73.67
## 61 2010 27.73 1127.56 58.34 84.62
## 62 2011 27.23 985.73 57.11 78.01
## 63 2012 27.00 1193.53 60.12 86.87
## 64 2013 27.26 1053.83 60.34 83.37
## 65 2014 27.11 1176.60 60.20 95.08
## 66 2015 27.27 1030.93 55.89 97.29
## 67 2016 27.41 1178.99 58.75 107.64
## 68 2017 27.39 1052.20 56.86 109.74
## 69 2018 27.19 1081.44 58.64 114.88
## 70 2019 27.25 1264.21 60.30 109.64
## 71 2020 27.26 1001.08 57.81 112.71
## 72 2021 27.50 888.86 57.26 118.31
## livestock_prod_index fish_prod_tons gdp_lcu agri_value_added_lcu
## 32 42.52 260132 1.39311e+11 1.705218e+10
## 33 45.88 269715 1.49051e+11 2.012592e+10
## 34 50.05 272312 1.58750e+11 2.379782e+10
## 35 51.70 263933 1.65854e+11 3.036518e+10
## 36 57.97 245353 1.87831e+11 3.423709e+10
## 37 50.99 271573 1.98123e+11 3.570264e+10
## 38 52.07 260808 2.44680e+11 5.028694e+10
## 39 50.34 279514 3.15615e+11 7.376451e+10
## 40 49.55 300449 4.14861e+11 8.826413e+10
## 41 50.35 316328 4.94644e+11 1.066270e+11
## 42 50.04 267705 5.90060e+11 1.232360e+11
## 43 50.77 318415 9.06029e+11 1.841160e+11
## 44 55.24 255499 1.25717e+12 2.953250e+11
## 45 60.22 281232 1.76879e+12 4.452730e+11
## 46 61.94 366101 3.10024e+12 7.901420e+11
## 47 63.55 357484 4.08607e+12 1.070510e+12
## 48 65.90 412220 4.41871e+12 1.211460e+12
## 49 71.91 483482 4.80516e+12 1.341040e+12
## 50 75.19 477365 5.48235e+12 1.426970e+12
## 51 74.71 467095 7.06275e+12 1.508410e+12
## 52 81.10 476544 8.23449e+12 2.015420e+12
## 53 85.71 511719 1.15015e+13 4.251520e+12
## 54 83.38 505839 1.35570e+13 4.585930e+12
## 55 86.49 509201 1.81241e+13 4.935260e+12
## 56 87.33 579537 2.31219e+13 6.032330e+12
## 57 85.91 636901 3.03752e+13 7.513300e+12
## 58 92.88 615507 3.46759e+13 8.551980e+12
## 59 95.14 744575 3.99542e+13 1.010030e+13
## 60 98.70 751006 4.34615e+13 1.162540e+13
## 61 98.36 817516 5.46123e+13 1.304890e+13
## 62 95.04 856614 6.31347e+13 1.403780e+13
## 63 90.28 922652 7.23515e+13 1.581600e+13
## 64 98.47 1000061 8.10100e+13 1.681660e+13
## 65 99.84 1073059 9.01370e+13 1.801860e+13
## 66 101.97 1027058 9.51777e+13 1.963700e+13
## 67 98.18 1041498 1.02575e+14 2.152350e+13
## 68 100.67 1212475 1.14899e+14 2.395260e+13
## 69 103.97 1169478 1.29087e+14 2.737130e+13
## 70 108.44 1114556 1.45639e+14 3.190410e+13
## 71 111.48 1044812 1.54252e+14 3.724160e+13
## 72 111.54 1080855 1.76076e+14 4.112610e+13
## agri_land_sqkm
## 32 566490
## 33 567580
## 34 568660
## 35 571730
## 36 574820
## 37 576900
## 38 577730
## 39 579640
## 40 581010
## 41 587140
## 42 594350
## 43 605960
## 44 609570
## 45 619180
## 46 640790
## 47 642400
## 48 644000
## 49 652110
## 50 653600
## 51 655080
## 52 656670
## 53 658080
## 54 659570
## 55 661030
## 56 662560
## 57 663970
## 58 665460
## 59 666960
## 60 668430
## 61 669940
## 62 671430
## 63 672930
## 64 674420
## 65 675930
## 66 677370
## 67 678910
## 68 680390
## 69 681920
## 70 683420
## 71 684930
## 72 686440
nrow(Clean_Ng1) # Numbers of Variables
## [1] 41
ncol(Clean_Ng1) #Numbers of Records
## [1] 10
glimpse(Clean_Ng1) #To identify values in embedded on my data.
## Rows: 41
## Columns: 10
## $ year <int> 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1…
## $ avg_temp_c <dbl> 26.40, 26.56, 26.85, 26.82, 26.59, 26.76, 27.36, …
## $ precipitation_mm <dbl> 1128.72, 1073.85, 866.06, 1037.82, 1188.69, 1091.…
## $ rel_humidity <dbl> 58.43, 59.40, 54.37, 57.04, 57.96, 58.43, 55.33, …
## $ crop_prod_index <dbl> 21.81, 22.23, 22.16, 23.67, 25.09, 27.49, 28.16, …
## $ livestock_prod_index <dbl> 42.52, 45.88, 50.05, 51.70, 57.97, 50.99, 52.07, …
## $ fish_prod_tons <dbl> 260132, 269715, 272312, 263933, 245353, 271573, 2…
## $ gdp_lcu <dbl> 1.39311e+11, 1.49051e+11, 1.58750e+11, 1.65854e+1…
## $ agri_value_added_lcu <dbl> 1.705218e+10, 2.012592e+10, 2.379782e+10, 3.03651…
## $ agri_land_sqkm <int> 566490, 567580, 568660, 571730, 574820, 576900, 5…
#Scatter plot of year vs crop_prod_index
ggplot(Clean_Ng1,
mapping = aes(year,crop_prod_index))+
geom_point()+
labs()+
scale_color_colorblind()
# box plot of the data.
ggplot(Nig_data1, #BOXPLOT OF UNCLEANED DATA.,
#Clean_Ng1,
mapping = aes(year,crop_prod_index))+
geom_boxplot()
# histogram plot of the data
ggplot(Nig_data1,
#Clean_Ng1,
mapping =
aes(crop_prod_index))+
geom_histogram()
#Using the penguins data: identifying numbers of rows and columns
nrow(data)
## [1] 344
ncol(data)
## [1] 8
# Scatter plot of bill_dept_mm vs bill_length
ggplot(data,
mapping = aes(bill_depth_mm, bill_length_mm))+
geom_point()+
labs()+
scale_color_colorblind()
#it looks like there might be different species here - Let's try a different plot.
ggplot(data,
mapping = aes(bill_depth_mm,bill_length_mm))+
geom_point(aes(color = species, shape = species))+
labs(
title = 'Bill Depth (mm) vs Bill Length (mm)',
x = 'Bill Depth', y = 'Bill Length',
color = 'Species', shape = 'Species'
)+
scale_color_colorblind()
# Scatter plot of species vs bill length?
ggplot(data,
mapping = aes(species,bill_depth_mm))+
geom_point()
#Points are congested. Let's try box plot!
ggplot(data,
mapping = aes(species,bill_depth_mm)
)+
geom_boxplot()+
scale_fill_few()
#Throw some color in
ggplot(data,
mapping = aes(species,bill_depth_mm,fill = species))+
geom_boxplot()+
scale_fill_few()
#Note that if you run, ggplot(data)+geom_point(),it will yield an error because "geom_point doesn't know what variables to use.
#Here we shall add few arguments to clear missing values with na.rm. set it as TRUE.
ggplot(data,
mapping = aes(sex,body_mass_g,shape=species,color=species),na.rm=TRUE)+
geom_point()+
labs(
caption = "Data comes from palmerpenguins",
x='Sex', y='Body Mass (g)'
)
# Recreating the visualization
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point(mapping = aes(color = bill_depth_mm))+
geom_smooth(model = 'lm')
#x= flipper, y=body mass, three lines, one for each color(species). No standard errors.
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g,color = island))+
geom_point()+
geom_smooth(se=FALSE)
#Trying something
#So, let's divide it into different plots
ggplot(data,
aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
geom_point() +
facet_wrap(~island) #Separating the categories in the plot.
#understanding how to pass variables at the global and local levels.
ggplot(data,
mapping = aes(flipper_length_mm,body_mass_g))+
geom_point()+
geom_smooth() #Local level.
#Global level
ggplot()+
geom_point(
data,
mapping = aes(flipper_length_mm,body_mass_g)
)+
geom_smooth(
data,
mapping = aes(flipper_length_mm,body_mass_g)
)
Wednesday 29th Oct, 2025. Using the dplyr package in R (tidyverse) to Structure my data to a interest:
#Using both Base R Pipeline and the in-suit Pipeline in tidyverse package.
library(tidyverse)
# |> and %>%
Nig_data1 <- "C:/Users/hp/Downloads/nigeria_agricultural_economic_indicators_1950_2023.csv" %>% read.csv()
Nig_data2 <-"C:/Users/hp/Downloads/nigeria_agricultural_economic_indicators_1950_2023.csv" |> read.csv()
# Understanding my data
sum(is.na(Nig_data1)) # Identifying the number of missing values in my data.
## [1] 92
sum(!is.na(Nig_data1)) # Identifying those values that are not missing (present) in my data.
## [1] 648
ncol(Nig_data1)
## [1] 10
nrow(Nig_data1)
## [1] 74
glimpse(Nig_data1)
## Rows: 74
## Columns: 10
## $ year <int> 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1…
## $ avg_temp_c <dbl> 25.82, 25.86, 25.91, 25.94, 25.98, 25.81, 25.83, …
## $ precipitation_mm <dbl> 1060.28, 1220.57, 1140.11, 1090.55, 1031.32, 1133…
## $ rel_humidity <dbl> 57.75, 60.56, 58.29, 58.27, 59.03, 57.52, 57.70, …
## $ crop_prod_index <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 19.31…
## $ livestock_prod_index <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 17.19…
## $ fish_prod_tons <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 58510, 55…
## $ gdp_lcu <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 299726870…
## $ agri_value_added_lcu <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ agri_land_sqkm <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 54176…
# Cleaning and preparing the data
Clean_Ng1 <- na.omit(Nig_data1)
Clean_Ng1
## year avg_temp_c precipitation_mm rel_humidity crop_prod_index
## 32 1981 26.40 1128.72 58.43 21.81
## 33 1982 26.56 1073.85 59.40 22.23
## 34 1983 26.85 866.06 54.37 22.16
## 35 1984 26.82 1037.82 57.04 23.67
## 36 1985 26.59 1188.69 57.96 25.09
## 37 1986 26.76 1091.10 58.43 27.49
## 38 1987 27.36 1060.67 55.33 28.16
## 39 1988 26.80 1292.39 58.47 32.81
## 40 1989 26.24 1087.29 55.82 36.78
## 41 1990 27.02 1048.21 57.27 38.98
## 42 1991 26.68 1181.01 60.11 44.86
## 43 1992 26.24 1105.24 58.25 48.45
## 44 1993 26.81 1040.79 57.70 50.95
## 45 1994 26.48 1292.50 59.26 52.37
## 46 1995 26.71 1168.81 58.48 54.75
## 47 1996 26.81 1101.24 58.40 57.45
## 48 1997 26.80 1078.70 59.21 59.36
## 49 1998 27.39 1097.34 56.79 61.10
## 50 1999 26.88 1241.89 58.99 63.76
## 51 2000 26.66 1033.15 56.13 64.38
## 52 2001 26.73 998.16 56.64 63.49
## 53 2002 26.86 1051.55 57.70 66.64
## 54 2003 27.07 1007.59 58.43 70.19
## 55 2004 27.14 999.91 57.47 74.89
## 56 2005 27.50 988.48 57.50 79.41
## 57 2006 27.45 1008.70 56.66 85.08
## 58 2007 27.00 1040.05 57.01 78.37
## 59 2008 26.82 1087.07 56.78 83.65
## 60 2009 27.50 1037.43 58.28 73.67
## 61 2010 27.73 1127.56 58.34 84.62
## 62 2011 27.23 985.73 57.11 78.01
## 63 2012 27.00 1193.53 60.12 86.87
## 64 2013 27.26 1053.83 60.34 83.37
## 65 2014 27.11 1176.60 60.20 95.08
## 66 2015 27.27 1030.93 55.89 97.29
## 67 2016 27.41 1178.99 58.75 107.64
## 68 2017 27.39 1052.20 56.86 109.74
## 69 2018 27.19 1081.44 58.64 114.88
## 70 2019 27.25 1264.21 60.30 109.64
## 71 2020 27.26 1001.08 57.81 112.71
## 72 2021 27.50 888.86 57.26 118.31
## livestock_prod_index fish_prod_tons gdp_lcu agri_value_added_lcu
## 32 42.52 260132 1.39311e+11 1.705218e+10
## 33 45.88 269715 1.49051e+11 2.012592e+10
## 34 50.05 272312 1.58750e+11 2.379782e+10
## 35 51.70 263933 1.65854e+11 3.036518e+10
## 36 57.97 245353 1.87831e+11 3.423709e+10
## 37 50.99 271573 1.98123e+11 3.570264e+10
## 38 52.07 260808 2.44680e+11 5.028694e+10
## 39 50.34 279514 3.15615e+11 7.376451e+10
## 40 49.55 300449 4.14861e+11 8.826413e+10
## 41 50.35 316328 4.94644e+11 1.066270e+11
## 42 50.04 267705 5.90060e+11 1.232360e+11
## 43 50.77 318415 9.06029e+11 1.841160e+11
## 44 55.24 255499 1.25717e+12 2.953250e+11
## 45 60.22 281232 1.76879e+12 4.452730e+11
## 46 61.94 366101 3.10024e+12 7.901420e+11
## 47 63.55 357484 4.08607e+12 1.070510e+12
## 48 65.90 412220 4.41871e+12 1.211460e+12
## 49 71.91 483482 4.80516e+12 1.341040e+12
## 50 75.19 477365 5.48235e+12 1.426970e+12
## 51 74.71 467095 7.06275e+12 1.508410e+12
## 52 81.10 476544 8.23449e+12 2.015420e+12
## 53 85.71 511719 1.15015e+13 4.251520e+12
## 54 83.38 505839 1.35570e+13 4.585930e+12
## 55 86.49 509201 1.81241e+13 4.935260e+12
## 56 87.33 579537 2.31219e+13 6.032330e+12
## 57 85.91 636901 3.03752e+13 7.513300e+12
## 58 92.88 615507 3.46759e+13 8.551980e+12
## 59 95.14 744575 3.99542e+13 1.010030e+13
## 60 98.70 751006 4.34615e+13 1.162540e+13
## 61 98.36 817516 5.46123e+13 1.304890e+13
## 62 95.04 856614 6.31347e+13 1.403780e+13
## 63 90.28 922652 7.23515e+13 1.581600e+13
## 64 98.47 1000061 8.10100e+13 1.681660e+13
## 65 99.84 1073059 9.01370e+13 1.801860e+13
## 66 101.97 1027058 9.51777e+13 1.963700e+13
## 67 98.18 1041498 1.02575e+14 2.152350e+13
## 68 100.67 1212475 1.14899e+14 2.395260e+13
## 69 103.97 1169478 1.29087e+14 2.737130e+13
## 70 108.44 1114556 1.45639e+14 3.190410e+13
## 71 111.48 1044812 1.54252e+14 3.724160e+13
## 72 111.54 1080855 1.76076e+14 4.112610e+13
## agri_land_sqkm
## 32 566490
## 33 567580
## 34 568660
## 35 571730
## 36 574820
## 37 576900
## 38 577730
## 39 579640
## 40 581010
## 41 587140
## 42 594350
## 43 605960
## 44 609570
## 45 619180
## 46 640790
## 47 642400
## 48 644000
## 49 652110
## 50 653600
## 51 655080
## 52 656670
## 53 658080
## 54 659570
## 55 661030
## 56 662560
## 57 663970
## 58 665460
## 59 666960
## 60 668430
## 61 669940
## 62 671430
## 63 672930
## 64 674420
## 65 675930
## 66 677370
## 67 678910
## 68 680390
## 69 681920
## 70 683420
## 71 684930
## 72 686440
# Using the function filter on the Clean data.Filter works on a data based on provided conditions in the argument Box.
A <- Clean_Ng1 %>% filter(# placeholder
.,# Condition
Clean_Ng1$year<2016)
A
## year avg_temp_c precipitation_mm rel_humidity crop_prod_index
## 1 1981 26.40 1128.72 58.43 21.81
## 2 1982 26.56 1073.85 59.40 22.23
## 3 1983 26.85 866.06 54.37 22.16
## 4 1984 26.82 1037.82 57.04 23.67
## 5 1985 26.59 1188.69 57.96 25.09
## 6 1986 26.76 1091.10 58.43 27.49
## 7 1987 27.36 1060.67 55.33 28.16
## 8 1988 26.80 1292.39 58.47 32.81
## 9 1989 26.24 1087.29 55.82 36.78
## 10 1990 27.02 1048.21 57.27 38.98
## 11 1991 26.68 1181.01 60.11 44.86
## 12 1992 26.24 1105.24 58.25 48.45
## 13 1993 26.81 1040.79 57.70 50.95
## 14 1994 26.48 1292.50 59.26 52.37
## 15 1995 26.71 1168.81 58.48 54.75
## 16 1996 26.81 1101.24 58.40 57.45
## 17 1997 26.80 1078.70 59.21 59.36
## 18 1998 27.39 1097.34 56.79 61.10
## 19 1999 26.88 1241.89 58.99 63.76
## 20 2000 26.66 1033.15 56.13 64.38
## 21 2001 26.73 998.16 56.64 63.49
## 22 2002 26.86 1051.55 57.70 66.64
## 23 2003 27.07 1007.59 58.43 70.19
## 24 2004 27.14 999.91 57.47 74.89
## 25 2005 27.50 988.48 57.50 79.41
## 26 2006 27.45 1008.70 56.66 85.08
## 27 2007 27.00 1040.05 57.01 78.37
## 28 2008 26.82 1087.07 56.78 83.65
## 29 2009 27.50 1037.43 58.28 73.67
## 30 2010 27.73 1127.56 58.34 84.62
## 31 2011 27.23 985.73 57.11 78.01
## 32 2012 27.00 1193.53 60.12 86.87
## 33 2013 27.26 1053.83 60.34 83.37
## 34 2014 27.11 1176.60 60.20 95.08
## 35 2015 27.27 1030.93 55.89 97.29
## livestock_prod_index fish_prod_tons gdp_lcu agri_value_added_lcu
## 1 42.52 260132 1.39311e+11 1.705218e+10
## 2 45.88 269715 1.49051e+11 2.012592e+10
## 3 50.05 272312 1.58750e+11 2.379782e+10
## 4 51.70 263933 1.65854e+11 3.036518e+10
## 5 57.97 245353 1.87831e+11 3.423709e+10
## 6 50.99 271573 1.98123e+11 3.570264e+10
## 7 52.07 260808 2.44680e+11 5.028694e+10
## 8 50.34 279514 3.15615e+11 7.376451e+10
## 9 49.55 300449 4.14861e+11 8.826413e+10
## 10 50.35 316328 4.94644e+11 1.066270e+11
## 11 50.04 267705 5.90060e+11 1.232360e+11
## 12 50.77 318415 9.06029e+11 1.841160e+11
## 13 55.24 255499 1.25717e+12 2.953250e+11
## 14 60.22 281232 1.76879e+12 4.452730e+11
## 15 61.94 366101 3.10024e+12 7.901420e+11
## 16 63.55 357484 4.08607e+12 1.070510e+12
## 17 65.90 412220 4.41871e+12 1.211460e+12
## 18 71.91 483482 4.80516e+12 1.341040e+12
## 19 75.19 477365 5.48235e+12 1.426970e+12
## 20 74.71 467095 7.06275e+12 1.508410e+12
## 21 81.10 476544 8.23449e+12 2.015420e+12
## 22 85.71 511719 1.15015e+13 4.251520e+12
## 23 83.38 505839 1.35570e+13 4.585930e+12
## 24 86.49 509201 1.81241e+13 4.935260e+12
## 25 87.33 579537 2.31219e+13 6.032330e+12
## 26 85.91 636901 3.03752e+13 7.513300e+12
## 27 92.88 615507 3.46759e+13 8.551980e+12
## 28 95.14 744575 3.99542e+13 1.010030e+13
## 29 98.70 751006 4.34615e+13 1.162540e+13
## 30 98.36 817516 5.46123e+13 1.304890e+13
## 31 95.04 856614 6.31347e+13 1.403780e+13
## 32 90.28 922652 7.23515e+13 1.581600e+13
## 33 98.47 1000061 8.10100e+13 1.681660e+13
## 34 99.84 1073059 9.01370e+13 1.801860e+13
## 35 101.97 1027058 9.51777e+13 1.963700e+13
## agri_land_sqkm
## 1 566490
## 2 567580
## 3 568660
## 4 571730
## 5 574820
## 6 576900
## 7 577730
## 8 579640
## 9 581010
## 10 587140
## 11 594350
## 12 605960
## 13 609570
## 14 619180
## 15 640790
## 16 642400
## 17 644000
## 18 652110
## 19 653600
## 20 655080
## 21 656670
## 22 658080
## 23 659570
## 24 661030
## 25 662560
## 26 663970
## 27 665460
## 28 666960
## 29 668430
## 30 669940
## 31 671430
## 32 672930
## 33 674420
## 34 675930
## 35 677370
B <- Clean_Ng1 %>% filter(.,Clean_Ng1$year<2016,Clean_Ng1$avg_temp_c<26.50)
# We can put more than two conditions in a filter function of the deplyr.
#More
C <- Clean_Ng1 %>% filter(.,Clean_Ng1$rel_humidity<=60,Clean_Ng1$precipitation_mm<890,Clean_Ng1$crop_prod_index>50)
#Using the base R pipeline.
C <- Clean_Ng1 |> filter(Clean_Ng1$year>=2018) # Doesn't needs a placeholder.
B <- Clean_Ng1 |> filter(Clean_Ng1$year>=2018,Clean_Ng1$precipitation_mm<900)
B
## year avg_temp_c precipitation_mm rel_humidity crop_prod_index
## 1 2021 27.5 888.86 57.26 118.31
## livestock_prod_index fish_prod_tons gdp_lcu agri_value_added_lcu
## 1 111.54 1080855 1.76076e+14 4.11261e+13
## agri_land_sqkm
## 1 686440
Friday 30th Oct, 2025. Using what I learnt with the filter function embedded in the deplyr part-package of tidyverse to run some time series analysis.
Getting started:
#install.packages(c("h2o","TSstudio","UKgrid",dependencies=TRUE))
library(forecast)
#library(h2o)
library(TSstudio)
library(plotly)
library(lubridate)
library(xts)
library(zoo)
library(UKgrid)
#Time series data is one of the most common formats of data, and it is used to describe an event or phenomena that occurs over time.Time series data has a simple requirement—its values need to be captured at equally spaced time intervals, such as seconds, minutes,hours, days, months, and so on. This important characteristic is one of the main attributes of the series and is known as the frequency of the series.
Summary of what was learnt from 23rd-31st October 2025.
1.Started chapter 10. (Conditions and loops)
2.Visualization using the ggplot package.
3.Tidying: Using deplyr package to tidy my data. Specifically using the function “filter” to reduce certain data to a desired form using several conditions e.g: filter(data,condition1, condition2,…). Before you do this make sure you first have done the following: library(tidyverse) # in order to update the systems environment with the needed package.
4.Began to study time series analysis. Text: Hands-On Time Series Analysis with R_perform Time series Analysis and Forecasting_Rami Krispin.