Play around with R Studio Layout. Go to Tools -> Global Options -> Appearance and Find a Style you like. Which one is it?
#getwd("C:\Users\etfie\OneDrive\Data Visualization\Assignment 1 Template.Rmd")
Find the name of your working directory
library(ggplot2)
Task: Compute the difference between 2024 and the year you started at this university and divide this by the difference between 2024 and the year you were born. Multiply this with 100 to get the percentage of your life you have spent at this university. Use brackets if you need them.
(2024-2023)/(2024-2005)*100
## [1] 5.263158
Task: Repeat the previous ToDo, but with several steps in between. You can give the variables any name you want, but the name has to start with a letter.
current = 2024
start_year = 2023
year_born = 2005
diff_college = current - start_year
diff_year = current - year_born
calc = (diff_college/ diff_year)*100
calc
## [1] 5.263158
fields = c(14, 7, 8, 1)
fields
## [1] 14 7 8 1
fields*2
## [1] 28 14 16 2
fields*fields
## [1] 196 49 64 1
(3+4+5)/3
## [1] 4
Compute the sum of 4, 5, 8 and 11 by first combining them into a vector and then using the function sum.
b=c(4,5,8,11)
#sum of the numbers
sum(b)
## [1] 28
Plot 100 normal random numbers.
x = rnorm(100)
plot(x)
Find help for the sqrt function.
help(sqrt)
## starting httpd help server ... done
Make a file called firstscript.R containing R-code that generates 100 random numbers and plots them, and run this script several times.
source("firstscript.R")
source("firstscript.R")
source("firstscript.R")
source("firstscript.R")
Run the demo code but with the following vectors. Before you run the code, see what you can predict what the results will be.
vec1 = c(1,3,5,7,9,11)
vec2 = seq(from=10, to=20, by=2)
Put the numbers 31 to 60 in a vector named P and in a matrix with 6 rows and 5 columns named Q. Tip: use the function seq. Look at the different ways scalars, vectors and matrices are denoted in the workspace window.
mat=matrix(data = c(31:60), ncol = 5, nrow = 6)
mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 31 37 43 49 55
## [2,] 32 38 44 50 56
## [3,] 33 39 45 51 57
## [4,] 34 40 46 52 58
## [5,] 35 41 47 53 59
## [6,] 36 42 48 54 60
Make a script file which constructs three random normal vectors of length 100. Call these vectors x1, x2 and x3. Make a data frame called t with three columns (called a, b and c) containing respectively x1, x1+x2 and x1+x2+x3.
x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
t = data.frame(x = c(x1), y = c(x1+x2), z = (x1+x2+x3))
t
## x y z
## 1 0.200199350 0.2165805568 -2.023648111
## 2 -0.334868427 -0.3598993035 -1.644599639
## 3 -1.782067316 -1.7286276521 -1.774032172
## 4 -1.177568916 -3.0762244166 -2.224956719
## 5 0.302239862 0.0149022656 0.516010825
## 6 0.212758169 1.4907667034 3.042671636
## 7 0.665794298 0.8402236581 -1.448637104
## 8 0.507422216 1.1430538799 0.659525605
## 9 1.183727497 1.7658668599 1.895718690
## 10 2.384884013 2.0375118477 1.433556841
## 11 -0.292185564 -0.3443958137 -0.655227397
## 12 0.440108928 2.3146146341 2.300897670
## 13 -0.101743227 -1.0282061701 -0.310591250
## 14 -0.196668143 -0.5000928666 -2.055133857
## 15 0.885281137 0.3948037371 1.198497269
## 16 0.791524049 1.0526918387 2.353158744
## 17 0.188300656 -0.5106044292 -2.102502726
## 18 0.383090464 -0.4121043032 -1.523200706
## 19 0.581609128 1.4906066103 1.790923001
## 20 1.129109750 4.2092025700 2.696341798
## 21 -1.026780921 0.2803157221 -0.543262013
## 22 -0.571811586 -1.8291336580 -0.263967685
## 23 -0.319219953 0.0197362133 -0.844793063
## 24 0.217585657 2.2371986329 2.162681826
## 25 -0.083580927 -1.4821747313 -0.606584658
## 26 -0.663439986 -2.9926801298 -3.802855339
## 27 -0.407941739 -1.9179236831 -1.270801562
## 28 -0.351509434 -1.4757712997 -0.684076831
## 29 1.245190902 1.2161113077 0.644486446
## 30 0.424823736 -0.4647968755 -1.871697980
## 31 -1.135029959 -2.6551149956 -3.173167283
## 32 -0.136905276 0.7727039867 -0.938991128
## 33 0.557994782 0.8443899886 1.278351642
## 34 1.343854383 1.7307904537 3.309143915
## 35 0.508598383 -0.5122356440 -0.739332156
## 36 -1.451069987 -2.6974521444 -3.115682716
## 37 -0.109528162 0.8236546238 -0.183503685
## 38 -0.194465014 -1.2265772081 0.841543874
## 39 -1.933833496 -0.5789297159 -0.328535208
## 40 0.265716296 0.4557914295 2.317237018
## 41 0.720665721 0.0741497348 -1.368547690
## 42 -0.639569388 -0.7520268429 -0.091551660
## 43 -1.334709504 -2.2806062340 -3.503899973
## 44 -1.444295257 -0.0066319456 0.803674740
## 45 1.100368220 -0.0721585284 0.834117529
## 46 -0.125796105 -1.2226085003 -0.960989124
## 47 0.990928841 0.9167750815 -1.510017818
## 48 1.410907058 0.1450685580 0.547978718
## 49 0.734724753 -2.2232202005 -2.842658509
## 50 0.371321713 0.8013127160 1.531636555
## 51 0.145535767 0.4746619283 0.018132984
## 52 0.483874924 1.7397737544 4.025605061
## 53 1.436598707 0.3872835781 -0.008514026
## 54 0.600151661 1.3494864518 1.870340773
## 55 -0.861517773 -1.3809076162 -2.128123433
## 56 0.672824654 -0.0007854562 -0.740961905
## 57 -0.864062458 -0.2050332322 0.721897231
## 58 0.151498325 0.5267514465 0.793773753
## 59 1.753842689 1.7752150177 1.209732300
## 60 -0.734347707 -1.9128124320 -2.875889033
## 61 -0.658982924 1.0161186917 1.540878446
## 62 -1.134391883 -0.3529833482 0.352478434
## 63 -0.675152816 0.7538396570 2.207648168
## 64 1.266125670 1.0266243142 0.279491648
## 65 -0.412509480 0.8282979811 2.079770040
## 66 1.719112962 2.2229282772 3.398689805
## 67 -0.837213256 0.6336525954 1.785404584
## 68 -0.754854345 -2.3847096840 -2.270985844
## 69 -1.059158308 -1.5488861322 -1.005528190
## 70 0.534743786 -1.1253051453 -0.912481434
## 71 1.361449972 1.2263551396 1.591390245
## 72 -0.725821341 -1.6979004972 -2.420336197
## 73 -0.872684465 -3.0049415351 -2.089801375
## 74 -1.194051050 -1.2661346107 0.489358925
## 75 -0.462986255 1.5495730233 1.702664791
## 76 -0.281557315 -0.2885277427 -0.658312178
## 77 -0.470421734 -0.6460591438 -0.261832872
## 78 1.192189969 1.4707535482 0.334487490
## 79 -1.245643543 -2.0330291824 -1.960204783
## 80 -1.241381291 -0.8464936018 -0.483001370
## 81 -0.743305730 -0.1745521071 -2.966616848
## 82 -0.720641000 -1.5161310904 -1.975599419
## 83 0.479963871 -0.6538951390 -0.789574650
## 84 -0.204870892 -1.8805019476 -2.253321251
## 85 -0.076767909 -1.4343384228 -3.342861705
## 86 -2.541897901 -1.2055337330 -3.230808632
## 87 1.080214316 0.5331769732 1.022469746
## 88 -0.172103197 0.5122058932 1.853415401
## 89 -0.802475704 1.1660805680 1.336347722
## 90 -0.836993104 -0.8497073799 -1.453724209
## 91 -0.334529256 -0.7774350447 -0.307069189
## 92 -0.008260752 1.1317230710 2.378139525
## 93 0.969039859 1.8227906669 3.668665698
## 94 0.574376713 1.3871570558 0.427858527
## 95 0.189186529 0.1974517034 0.049329150
## 96 -0.114669260 0.1296575771 1.672981539
## 97 -0.212583664 -0.7520832720 1.042436352
## 98 1.860259489 3.8026590781 3.901002982
## 99 0.631902713 -1.3599669371 -3.562202324
## 100 -1.008171154 -0.1728906634 -0.406859725
Call the following functions for this data frame: plot(t) and sd(t). Can you understand the results? Rerun this script a few times.
Add these lines to the script file of the previous section. Try to find out, either by experimenting or by using the help, what the meaning is of rgb, the last argument of rgb, lwd, pch, cex.
Make a file called tst1.txt in Notepad from the example in Figure 4 and store it in your working directory. Write a script to read it, to multiply the column called g by 5 and to store it as tst2.txt.
Compute the mean of the square root of a vector of 100 random numbers. What happens?
mean(sqrt( rnorm(100) ))
## Warning in sqrt(rnorm(100)): NaNs produced
## [1] NaN
Make a graph with on the x-axis: today, Sinterklaas (Christmas) 2014 and your next birthday and on the y-axis the number of presents you expect on each of these days. Tip: make two vectors first.
Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply the elements which are smaller than 5 and larger than 90 with 10 and the other elements with 0.1.
Write a function for the previous ToDo, so that you can feed it any vector you like (as argument). Use a for-loop in the function to do the computation with each element. Use the standard R function length in the specifcation of the counter.