Load the csv dataset from the working directory

df<-read.csv("Data_Task.csv")

Keep only the abd_lgth which is greater than 0.5 month

months_in_army<-subset(df, abd_lgth>0.5, select=abd_lgth)

Represent the descriptive statistics like Mean, Median, and the Quartiles and a Histogram

summary(months_in_army)
##     abd_lgth       
##  Min.   :  0.5333  
##  1st Qu.:  2.7500  
##  Median :  7.0000  
##  Mean   : 14.3780  
##  3rd Qu.: 18.0000  
##  Max.   :137.0000
hist(as.matrix(months_in_army), main="Histogram of Months in the Army", xlab = "Months", ylab="Number of Former Combatants", col="green", breaks=100, ylim=c(0,50), xlim=c(0.5,140))

Create the allegiance_comb variable

df$allegiance_comb<-df$allegiance_freq

for (i in 1:length(df$allegiance_comb)) {

if (is.na(df$ix48_allegiance[i])) {df$allegiance_comb[i]=df$allegiance_freq[i]}

else if (df$ix48_allegiance[i]==0) {

df$allegiance_comb[i]=0
} else

{df$allegiance_comb[i]=df$allegiance_freq[i]}


}

Create the stay_comb variable

df$stay_comb<-df$stay_freq

for (i in 1:length(df$stay_comb)) {

if (is.na(df$ix51_stay[i])) {df$stay_comb[i]=df$stay_freq[i]}

else if (df$ix51_stay[i]==0) {

df$stay_comb[i]=0
} else

{df$stay_comb[i]=df$stay_freq[i]}


}

Keep a dataset with the “id”, important" “allegiance_comb”, and “stay_comb” columns

df_reduced<-subset(df, select=c(id,important, allegiance_comb, stay_comb ))

Now we need to do apply “case-wise deletion” meaning that if there is any missing value in the data we remove them

df_completed<-df_reduced[complete.cases(df_reduced),]

## As we can see we have only 294 Soldiers with non missing values in any of the three fields 

nrow(df_completed)
## [1] 294

Now we can calculate the mean of each column

apply(df_completed[,c(-1)], 2, mean)
##       important allegiance_comb       stay_comb 
##       1.7244898       1.2823129       0.4931973

Now we can calculate the Average of each Soldier based on the three columns which is the index

###Calculate the mean
Average<-(apply(df_completed[,c(-1)], 1, mean))


### Represent the results in a data frame
data.frame(df_completed, Average)
##      id important allegiance_comb stay_comb   Average
## 5     5         3               0         0 1.0000000
## 7     7         0               0         0 0.0000000
## 9     9         2               3         0 1.6666667
## 11   11         2               0         0 0.6666667
## 15   15         0               0         0 0.0000000
## 17   17         0               0         0 0.0000000
## 20   20         3               3         0 2.0000000
## 21   21         2               3         0 1.6666667
## 22   22         0               0         0 0.0000000
## 23   23         3               3         0 2.0000000
## 24   24         3               3         1 2.3333333
## 27   27         2               3         0 1.6666667
## 28   28         3               3         2 2.6666667
## 29   29         0               3         0 1.0000000
## 30   30         3               3         0 2.0000000
## 32   32         0               1         0 0.3333333
## 35   35         3               1         0 1.3333333
## 37   37         0               0         0 0.0000000
## 39   39         3               0         0 1.0000000
## 40   40         2               3         0 1.6666667
## 41   41         0               1         0 0.3333333
## 44   44         1               3         0 1.3333333
## 45   45         3               3         0 2.0000000
## 47   47         0               0         0 0.0000000
## 48   48         3               3         0 2.0000000
## 51   51         3               0         3 2.0000000
## 57   57         1               1         0 0.6666667
## 58   58         3               3         2 2.6666667
## 59   59         1               1         0 0.6666667
## 60   60         3               3         0 2.0000000
## 61   61         0               2         0 0.6666667
## 62   62         0               0         0 0.0000000
## 63   63         0               0         0 0.0000000
## 64   64         0               1         0 0.3333333
## 65   65         0               0         0 0.0000000
## 66   66         0               3         0 1.0000000
## 67   67         3               3         0 2.0000000
## 69   69         2               3         0 1.6666667
## 71   71         3               3         0 2.0000000
## 74   74         2               0         0 0.6666667
## 75   75         0               1         0 0.3333333
## 77   77         3               0         3 2.0000000
## 78   78         3               0         0 1.0000000
## 79   79         0               3         0 1.0000000
## 80   80         0               1         0 0.3333333
## 85   85         0               0         0 0.0000000
## 87   87         3               0         0 1.0000000
## 89   89         3               2         0 1.6666667
## 91   91         3               3         0 2.0000000
## 92   92         1               0         0 0.3333333
## 96   96         3               0         3 2.0000000
## 98   98         0               3         0 1.0000000
## 100 100         0               3         0 1.0000000
## 101 101         3               0         0 1.0000000
## 102 102         2               2         0 1.3333333
## 106 106         2               3         0 1.6666667
## 107 107         0               3         0 1.0000000
## 121 122         1               0         3 1.3333333
## 127 128         2               0         0 0.6666667
## 132 133         3               0         0 1.0000000
## 133 134         0               3         0 1.0000000
## 136 137         2               3         0 1.6666667
## 137 138         3               3         0 2.0000000
## 138 139         0               0         0 0.0000000
## 141 142         0               3         0 1.0000000
## 143 144         0               0         0 0.0000000
## 144 145         3               3         0 2.0000000
## 145 146         0               0         0 0.0000000
## 148 149         3               3         0 2.0000000
## 150 151         0               0         0 0.0000000
## 151 152         2               0         0 0.6666667
## 152 153         3               3         0 2.0000000
## 154 155         3               3         0 2.0000000
## 157 158         3               0         0 1.0000000
## 158 159         2               1         0 1.0000000
## 159 160         3               3         1 2.3333333
## 160 161         3               3         0 2.0000000
## 162 163         3               0         0 1.0000000
## 163 164         3               2         0 1.6666667
## 165 166         0               3         0 1.0000000
## 166 167         3               3         3 3.0000000
## 168 169         3               2         0 1.6666667
## 170 171         0               3         0 1.0000000
## 171 172         1               3         0 1.3333333
## 173 174         0               1         0 0.3333333
## 174 175         2               3         0 1.6666667
## 175 176         3               0         0 1.0000000
## 176 177         3               3         3 3.0000000
## 177 178         0               0         0 0.0000000
## 178 179         0               3         0 1.0000000
## 180 181         2               1         2 1.6666667
## 181 182         1               0         0 0.3333333
## 182 183         3               0         0 1.0000000
## 183 184         3               3         0 2.0000000
## 187 188         0               0         0 0.0000000
## 188 189         1               0         0 0.3333333
## 189 190         0               3         0 1.0000000
## 190 191         0               0         0 0.0000000
## 195 196         1               1         0 0.6666667
## 196 197         2               0         1 1.0000000
## 197 198         0               1         0 0.3333333
## 198 199         0               0         0 0.0000000
## 202 203         3               1         1 1.6666667
## 203 204         3               0         0 1.0000000
## 209 210         0               1         0 0.3333333
## 210 211         2               0         0 0.6666667
## 214 215         0               0         0 0.0000000
## 216 217         3               2         1 2.0000000
## 218 219         2               3         0 1.6666667
## 220 221         3               0         0 1.0000000
## 221 222         2               0         0 0.6666667
## 222 223         0               0         0 0.0000000
## 224 225         3               3         3 3.0000000
## 227 228         2               0         0 0.6666667
## 228 229         0               0         0 0.0000000
## 231 232         0               0         2 0.6666667
## 233 234         0               1         0 0.3333333
## 234 235         0               0         0 0.0000000
## 235 236         0               0         0 0.0000000
## 236 237         3               0         3 2.0000000
## 237 238         3               0         0 1.0000000
## 238 239         0               0         0 0.0000000
## 240 241         0               3         0 1.0000000
## 241 242         1               0         0 0.3333333
## 242 243         1               3         0 1.3333333
## 247 248         2               3         0 1.6666667
## 249 250         2               3         0 1.6666667
## 252 253         0               3         0 1.0000000
## 253 254         0               0         0 0.0000000
## 256 257         3               0         3 2.0000000
## 257 258         2               0         0 0.6666667
## 258 259         2               0         0 0.6666667
## 260 261         0               0         0 0.0000000
## 261 262         1               3         0 1.3333333
## 263 264         0               3         1 1.3333333
## 264 265         3               0         0 1.0000000
## 265 266         1               0         0 0.3333333
## 266 267         0               0         0 0.0000000
## 267 268         3               1         0 1.3333333
## 272 273         0               1         1 0.6666667
## 275 276         1               0         1 0.6666667
## 280 281         3               3         0 2.0000000
## 281 282         0               3         0 1.0000000
## 284 285         0               0         0 0.0000000
## 285 286         1               1         0 0.6666667
## 286 287         2               3         0 1.6666667
## 287 288         2               3         0 1.6666667
## 288 289         2               3         2 2.3333333
## 289 300         2               0         0 0.6666667
## 290 301         3               3         0 2.0000000
## 303 314         0               0         3 1.0000000
## 310 321         1               3         3 2.3333333
## 315 326         3               2         0 1.6666667
## 331 342         2               0         0 0.6666667
## 333 344         2               0         0 0.6666667
## 337 348         0               3         0 1.0000000
## 340 351         3               2         0 1.6666667
## 341 352         1               0         0 0.3333333
## 345 356         3               2         0 1.6666667
## 346 357         0               0         0 0.0000000
## 348 359         3               1         0 1.3333333
## 349 360         0               0         1 0.3333333
## 354 365         3               3         0 2.0000000
## 360 371         3               0         3 2.0000000
## 361 372         0               0         0 0.0000000
## 364 375         0               1         0 0.3333333
## 367 378         0               0         2 0.6666667
## 368 379         0               0         1 0.3333333
## 372 383         0               0         0 0.0000000
## 374 385         0               3         0 1.0000000
## 375 386         0               1         1 0.6666667
## 376 387         0               0         0 0.0000000
## 377 388         3               0         0 1.0000000
## 378 389         0               0         0 0.0000000
## 380 391         2               2         0 1.3333333
## 381 392         2               0         0 0.6666667
## 382 393         1               3         0 1.3333333
## 384 395         1               0         0 0.3333333
## 387 398         3               3         2 2.6666667
## 389 400         2               0         0 0.6666667
## 394 405         2               3         0 1.6666667
## 397 408         2               3         0 1.6666667
## 400 411         3               3         0 2.0000000
## 403 414         0               0         3 1.0000000
## 404 415         3               3         3 3.0000000
## 407 418         1               2         0 1.0000000
## 411 422         3               0         0 1.0000000
## 413 424         3               0         0 1.0000000
## 414 425         3               1         1 1.6666667
## 421 432         3               0         3 2.0000000
## 423 434         3               3         0 2.0000000
## 425 436         2               0         3 1.6666667
## 434 445         2               0         2 1.3333333
## 435 446         3               0         0 1.0000000
## 438 449         2               0         0 0.6666667
## 444 455         3               0         0 1.0000000
## 445 456         3               3         2 2.6666667
## 447 458         1               2         0 1.0000000
## 451 462         2               3         0 1.6666667
## 456 467         3               3         0 2.0000000
## 457 468         3               3         0 2.0000000
## 458 469         3               3         0 2.0000000
## 463 474         2               3         0 1.6666667
## 467 478         0               0         0 0.0000000
## 468 479         0               0         0 0.0000000
## 473 484         2               3         0 1.6666667
## 477 488         3               0         3 2.0000000
## 480 491         3               0         0 1.0000000
## 483 494         0               1         0 0.3333333
## 486 497         3               0         2 1.6666667
## 498 509         3               3         1 2.3333333
## 500 511         3               3         3 3.0000000
## 504 515         2               3         0 1.6666667
## 506 517         0               0         0 0.0000000
## 509 520         0               2         3 1.6666667
## 511 522         3               0         0 1.0000000
## 512 523         3               2         0 1.6666667
## 516 527         3               1         0 1.3333333
## 521 532         2               0         2 1.3333333
## 522 533         3               0         3 2.0000000
## 523 534         3               0         0 1.0000000
## 526 537         2               0         0 0.6666667
## 528 539         1               0         3 1.3333333
## 538 549         3               1         3 2.3333333
## 539 550         3               1         0 1.3333333
## 540 551         3               1         0 1.3333333
## 541 552         2               1         0 1.0000000
## 547 558         3               1         0 1.3333333
## 549 560         0               0         0 0.0000000
## 550 561         2               0         0 0.6666667
## 551 562         3               1         0 1.3333333
## 552 563         3               3         1 2.3333333
## 553 564         0               2         0 0.6666667
## 556 567         3               2         0 1.6666667
## 565 576         3               0         3 2.0000000
## 566 577         0               0         0 0.0000000
## 567 578         2               2         2 2.0000000
## 568 579         3               3         0 2.0000000
## 569 580         2               3         0 1.6666667
## 573 584         3               3         0 2.0000000
## 587 598         3               0         0 1.0000000
## 590 601         0               0         0 0.0000000
## 591 602         0               3         0 1.0000000
## 593 604         3               1         0 1.3333333
## 599 610         2               0         0 0.6666667
## 600 611         0               0         3 1.0000000
## 601 612         0               3         0 1.0000000
## 602 613         3               3         0 2.0000000
## 607 618         1               0         3 1.3333333
## 609 620         3               0         0 1.0000000
## 610 621         3               0         0 1.0000000
## 616 627         3               0         0 1.0000000
## 631 642         2               0         3 1.6666667
## 634 645         3               0         1 1.3333333
## 637 648         1               1         0 0.6666667
## 638 649         3               3         1 2.3333333
## 640 651         3               1         0 1.3333333
## 642 653         3               3         1 2.3333333
## 643 654         3               0         0 1.0000000
## 648 659         3               3         3 3.0000000
## 652 663         3               3         0 2.0000000
## 653 664         1               3         0 1.3333333
## 660 671         0               2         0 0.6666667
## 668 679         3               3         3 3.0000000
## 669 680         0               0         0 0.0000000
## 671 682         3               2         0 1.6666667
## 678 689         3               1         2 2.0000000
## 679 690         2               0         0 0.6666667
## 680 691         3               0         3 2.0000000
## 684 695         0               0         2 0.6666667
## 685 696         2               3         0 1.6666667
## 686 697         3               0         0 1.0000000
## 690 701         3               2         3 2.6666667
## 696 707         1               0         0 0.3333333
## 698 709         2               2         0 1.3333333
## 705 716         3               0         3 2.0000000
## 710 721         0               2         1 1.0000000
## 711 722         0               2         0 0.6666667
## 712 723         3               3         0 2.0000000
## 713 724         3               0         0 1.0000000
## 714 725         0               0         0 0.0000000
## 716 727         0               0         0 0.0000000
## 720 731         3               0         0 1.0000000
## 721 732         2               3         0 1.6666667
## 722 733         3               3         0 2.0000000
## 723 734         3               0         0 1.0000000
## 724 735         3               0         3 2.0000000
## 726 737         3               0         0 1.0000000
## 730 741         3               0         0 1.0000000
## 732 743         3               0         0 1.0000000
## 734 745         2               0         0 0.6666667
## 735 746         2               2         0 1.3333333
## 738 749         3               0         3 2.0000000
## 741 753         3               3         0 2.0000000

Now we calculate the mean and the variance of the index

##The average
mean(apply(df_completed[,c(-1)], 1, sum))
## [1] 3.5
##The variance
var(apply(df_completed[,c(-1)], 1, sum))
## [1] 5.308874
##The standard deviationn
sqrt(var(apply(df_completed[,c(-1)], 1, sum)))
## [1] 2.304099

Given that the Soldier has stayed more than 0.5 months what is the Proportion of Kill_soldier

kill<-subset(df, abd_lgth>0.5 & !is.na(kill_soldier), select=kill_soldier)

###Frequencies
table(kill)
## kill
##   0   1 
## 260  71
###Percentages
table(kill)/dim(kill)[1]
## kill
##         0         1 
## 0.7854985 0.2145015
barplot(table(kill)/dim(kill)[1], main="Kill Soldier", col="red", ylim = c(0,1))

Given that the Soldier has stayed more than 0.5 months what is the Proportion of beat_family

beat<-subset(df, abd_lgth>0.5 & !is.na(beat_family), select=beat_family)

###Frequencies
table(beat)
## beat
##   0   1 
## 273  58
###Percentages
table(beat)/dim(beat)[1]
## beat
##         0         1 
## 0.8247734 0.1752266
barplot(table(beat)/dim(beat)[1], main="Beat Family", col="red", ylim = c(0,1))

Given that the Soldier has stayed more than 0.5 months what is the Proportion of beat_civilian

beat_c<-subset(df, abd_lgth>0.5 & !is.na(beat_civilian), select=beat_civilian)

###Frequencies
table(beat_c)
## beat_c
##   0   1 
## 237  94
###Percentages
table(beat_c)/dim(beat_c)[1]
## beat_c
##         0         1 
## 0.7160121 0.2839879
barplot(table(beat_c)/dim(beat_c)[1], main="Beat Civilian", col="red", ylim = c(0,1))

Given that the Soldier has stayed more than 0.5 months what is the Proportion of kill_family

kill_f<-subset(df, abd_lgth>0.5 & !is.na(kill_family), select=kill_family)

###Frequencies
table(kill_f)
## kill_f
##   0   1 
## 298  33
###Percentages
table(kill_f)/dim(kill_f)[1]
## kill_f
##          0          1 
## 0.90030211 0.09969789
barplot(table(kill_f)/dim(kill_f)[1], main="Kill Family", col="red", ylim = c(0,1))

Given that the Soldier has stayed more than 0.5 months what is the Proportion of kill_civilian

kill_c<-subset(df, abd_lgth>0.5 & !is.na(kill_civilian), select=kill_civilian)

###Frequencies
table(kill_c)
## kill_c
##   0   1 
## 246  85
###Percentages
table(kill_c)/dim(kill_c)[1]
## kill_c
##         0         1 
## 0.7432024 0.2567976
barplot(table(kill_c)/dim(kill_c)[1], main="Kill Civilian", col="red", ylim = c(0,1))

Given that the Soldier has stayed more than 0.5 months what is the Proportion of forced_sex

forced_s<-subset(df, abd_lgth>0.5 & !is.na(forced_sex), select=forced_sex)

###Frequencies
table(forced_s)
## forced_s
##   0   1 
## 320  11
###Percentages
table(forced_s)/dim(forced_s)[1]
## forced_s
##          0          1 
## 0.96676737 0.03323263
barplot(table(forced_s)/dim(forced_s)[1], main="Forced Sex", col="red", ylim = c(0,1))

All plots in one

forced_sex<-table(forced_s)/dim(forced_s)[1]
kill_civilian<-table(kill_c)/dim(kill_c)[1]
kill_family<-table(kill_f)/dim(kill_f)[1]
beat_civilian<-table(beat_c)/dim(beat_c)[1]
beat_family<-table(beat)/dim(beat)[1]
kill_soldier<-table(kill)/dim(kill)[1]

counts<-rbind(forced_sex, kill_civilian, kill_family, beat_civilian, beat_family, kill_soldier)

barplot(counts, main="Bar Plot", xlab="No Yes", legend=rownames(counts), beside=TRUE, ylim = c(0,1), col=c("darkblue","red", "green", "black", "grey", "yellow"))

A Barplot only with the percentage of 1

barplot(counts[,2],  main="Barplot", col=rainbow(6), las=2, ylim=c(0,0.3), cex.names=0.8)

A Barplot only with the percentage of 1

par(mar=c(5,8,4,2))
barplot(counts[,2],  main="Barplot", col=rainbow(6), las=2, xlim=c(0,0.3), horiz = TRUE )