10+5 # Sum
[1] 15
10-5 # Subtraction
[1] 5
4*3 # Multiplication
[1] 12
5^2 # Powers
[1] 25
10/5 # Division
[1] 2
5%%2 # Integer division, remainder
[1] 1
With = or with <-; the value on the right is assigned to the value on the left. - R is case sensitive (Sensible to lower and upper cases). - R does NOT require you to specify what kind of values the variables will contain.
(y = x+3)
[1] 7
getwd returns an absolute filepath representing the current working directory of the R process; setwd(dir) is used to set the working directory to dir.
getwd() # returns an absolute filepath representing the current working directory of the R process;
[1] "C:/Users/crice/Box/TEAM-Folder/PAPERS/Mathematical Modeling and Wastewater-Based Epidemiology Public Health Applications"
# echo = F don't show code
colnames = c("ARITHMETIC","COMPARATIVE","LOGICAL")
ARITHMETIC = c("+","-","*","/","^","%%")
Description1 = c("Addition","Subtraction","Multiplication","Division","Power","Integer Div")
LOGICAL = c("==","!=","<",">","<=",">=")
Description2 = c("Equal to","Different from", "Less than", "Greater than","Less than o equal to",
"Greater than o equal to")
COMPARATIVE = c("&","!","","is.na(x)","is.null(x)","s.nan(x)")
Description3 = c("Logical and","Logical no","","Is NA?","Is it Null?","Is it NaN?")
table = cbind(ARITHMETIC,Description1,LOGICAL,Description2,COMPARATIVE,Description3) # combine by columns (or by rows with rbind)
print(data.frame(table)) # Convert to data frame
ARITHMETIC <chr> | Description1 <chr> | LOGICAL <chr> | Description2 <chr> | COMPARATIVE <chr> | Description3 <chr> |
---|---|---|---|---|---|
+ | Addition | == | Equal to | & | Logical and |
- | Subtraction | != | Different from | ! | Logical no |
* | Multiplication | < | Less than | ||
/ | Division | > | Greater than | is.na(x) | Is NA? |
^ | Power | <= | Less than o equal to | is.null(x) | Is it Null? |
%% | Integer Div | >= | Greater than o equal to | s.nan(x) | Is it NaN? |
#library(DT)
#datatable(cbind(ARITHMETIC,description))
sprintf("x = %s and X = %s",x,X) # Combine string and numeric values
[1] "x = 4 and X = 5"
x == X # R is case sensitive
[1] FALSE
x != X
[1] TRUE
x <= X
[1] TRUE
x >= X
[1] FALSE
# First way
X <- c(1,2,3)
X
[1] 1 2 3
# Second way
X <- 1:3
X
[1] 1 2 3
# Third way
X1 = rep(1, times=3) # Repeat 1, 3 times
X1
[1] 1 1 1
vec = 1:3
X2 = rep(vec, times=5) # Repeat vec 5 times
X2
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
X3=rep(vec, each=3) #
X3
[1] 1 1 1 2 2 2 3 3 3
# Other way
a <- seq(from=5, to=23, by=.5 ) # a sequence of increase by
a
b <- seq(from=5, to=23, length=100) # a sequence of length 100
b
# Part I.- Basic objects
# Some objects: numbers, vector, matrices, data frames, array, lists
a <- 2 # Number
b = c(4,8,0,16) # vector
c = 1:14 # vector
d = matrix( 1:10, nrow=2, byrow=T ) # matrix
e = matrix( 1:10, ncol=2, byrow=F ) # matrix default by column
f = matrix( 1:10, ncol=5)
bm = matrix(b, nrow=2, byrow=T )
print(b)
[1] 4 8 0 16
dim(d) # dimension of d matrix
[1] 2 5
remove(d) # remove an object
ls() # lists all objects in the directory
[1] "a" "ARITHMETIC" "b" "bd" "bm" "c"
[7] "city" "city_ww" "colnames" "COMPARATIVE" "data" "Description1"
[13] "Description2" "Description3" "e" "f" "greeting" "LOGICAL"
[19] "table" "vec" "x" "X" "X1" "X2"
[25] "X3" "X4" "X5" "y" "Y"
remove(a,c) # remove multiples objects
remove( list=ls() ) # remove all objects
a <- seq(from=5, to=23, by=.5 ) # a sequence wint increase by (o vector)
a <- seq(from=5, to=23, length=100) # a sequence of length 100
b <- rnorm(100) # generate random values of a normal distribution
b2 <- runif(100) # generate random values of a uniform distribution
hist(b) # to lot histograms
plot(b,b2) # simple plot
c <- cbind(b,b2) # matrix; vertical concatination
ct <- t(c) # transpouse
c
b b2
[1,] -1.38088069 0.858264087
[2,] 0.66437838 0.003903936
[3,] -1.44192672 0.478081123
[4,] -0.23257360 0.918456087
[5,] -0.24671246 0.807689112
[6,] -0.86522403 0.792330434
[7,] -2.12005285 0.042114281
[8,] -0.13144326 0.194970421
[9,] -0.95537091 0.452283577
[10,] 0.75321983 0.438737167
[11,] -0.05489260 0.480610712
[12,] 1.69562835 0.392007038
[13,] -1.03830522 0.242866530
[14,] 1.18681120 0.099108091
[15,] 1.21178054 0.516976763
[16,] -1.74898496 0.065540741
[17,] -0.65038649 0.636335618
[18,] -0.58804841 0.232607744
[19,] -0.55933220 0.203854955
[20,] 0.19256130 0.090373779
[21,] -1.79065718 0.751115266
[22,] 0.15805978 0.361704670
[23,] -1.45530133 0.692347908
[24,] -0.37164945 0.248734678
[25,] -0.11791927 0.596868856
[26,] -0.02536543 0.809549505
[27,] 0.34387281 0.381528180
[28,] -0.77575216 0.634686878
[29,] 0.86905691 0.030431538
[30,] 0.38581730 0.193273982
[31,] -0.37279603 0.378746056
[32,] 0.44998849 0.578904315
[33,] -0.59157222 0.012211904
[34,] -1.50530259 0.407359428
[35,] 1.04699247 0.964360910
[36,] 0.12685366 0.216363109
[37,] -1.11519377 0.147212892
[38,] 0.10811769 0.932043133
[39,] -1.26673735 0.115032387
[40,] -0.50714586 0.999853693
[41,] -0.91868951 0.810800158
[42,] -0.29731871 0.178087981
[43,] 1.15975757 0.842401248
[44,] -0.26018242 0.071121373
[45,] 0.52578631 0.163020681
[46,] 0.24438166 0.710500215
[47,] 0.20889924 0.026670772
[48,] -1.93744720 0.169773897
[49,] -0.00695318 0.209948316
[50,] -0.71452340 0.034671564
[51,] 0.57562852 0.612801080
[52,] -1.23572577 0.623946457
[53,] -0.08100244 0.703801848
[54,] -0.09862191 0.540504236
[55,] 1.13933497 0.245369178
[56,] -0.06682206 0.546186707
[57,] -0.63028029 0.711188254
[58,] -0.44492628 0.899419523
[59,] -1.28421527 0.889601162
[60,] -0.07741493 0.841342199
[61,] 0.31113744 0.199085864
[62,] 0.40124242 0.295827813
[63,] 0.62060369 0.385276034
[64,] 0.13618069 0.503965177
[65,] 0.40326074 0.567528928
[66,] 0.12652018 0.972277519
[67,] -0.65713783 0.300787880
[68,] -0.33685661 0.615531536
[69,] 1.44036035 0.273717905
[70,] 0.46136827 0.123578306
[71,] -0.22996401 0.814653691
[72,] -1.76062685 0.038490191
[73,] 1.04426533 0.059043459
[74,] 0.98493336 0.452822033
[75,] -0.19937291 0.296500278
[76,] -2.19153774 0.178488104
[77,] 0.05498726 0.260125093
[78,] -0.80456289 0.488688321
[79,] 0.65374188 0.868612124
[80,] 2.55209448 0.208544536
[81,] -0.25933383 0.215808191
[82,] 1.23347332 0.485450778
[83,] -1.11345021 0.309516371
[84,] 0.08112593 0.727236132
[85,] -0.69672677 0.473619435
[86,] 0.60107763 0.842930949
[87,] -0.73421813 0.244173994
[88,] 2.08255031 0.848972967
[89,] -1.04482047 0.998221477
[90,] 0.75542726 0.419006828
[91,] 0.21755891 0.060381147
[92,] 0.72899249 0.527818844
[93,] -0.28522888 0.128960422
[94,] 0.79269156 0.963665575
[95,] -0.19227968 0.955944677
[96,] 2.90598740 0.115874609
[97,] 0.91663557 0.041463981
[98,] 1.28927133 0.471626976
[99,] -0.42781231 0.622513014
[100,] 0.35225873 0.151627695
ct
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
b -1.3808807 0.664378379 -1.4419267 -0.2325736 -0.2467125 -0.8652240 -2.12005285 -0.1314433 -0.9553709
b2 0.8582641 0.003903936 0.4780811 0.9184561 0.8076891 0.7923304 0.04211428 0.1949704 0.4522836
[,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
b 0.7532198 -0.0548926 1.695628 -1.0383052 1.18681120 1.2117805 -1.74898496 -0.6503865 -0.5880484
b2 0.4387372 0.4806107 0.392007 0.2428665 0.09910809 0.5169768 0.06554074 0.6363356 0.2326077
[,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27]
b -0.5593322 0.19256130 -1.7906572 0.1580598 -1.4553013 -0.3716494 -0.1179193 -0.02536543 0.3438728
b2 0.2038550 0.09037378 0.7511153 0.3617047 0.6923479 0.2487347 0.5968689 0.80954950 0.3815282
[,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
b -0.7757522 0.86905691 0.3858173 -0.3727960 0.4499885 -0.5915722 -1.5053026 1.0469925 0.1268537
b2 0.6346869 0.03043154 0.1932740 0.3787461 0.5789043 0.0122119 0.4073594 0.9643609 0.2163631
[,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
b -1.1151938 0.1081177 -1.2667373 -0.5071459 -0.9186895 -0.2973187 1.1597576 -0.26018242 0.5257863
b2 0.1472129 0.9320431 0.1150324 0.9998537 0.8108002 0.1780880 0.8424012 0.07112137 0.1630207
[,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54]
b 0.2443817 0.20889924 -1.9374472 -0.00695318 -0.71452340 0.5756285 -1.2357258 -0.08100244 -0.09862191
b2 0.7105002 0.02667077 0.1697739 0.20994832 0.03467156 0.6128011 0.6239465 0.70380185 0.54050424
[,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63]
b 1.1393350 -0.06682206 -0.6302803 -0.4449263 -1.2842153 -0.07741493 0.3111374 0.4012424 0.6206037
b2 0.2453692 0.54618671 0.7111883 0.8994195 0.8896012 0.84134220 0.1990859 0.2958278 0.3852760
[,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72]
b 0.1361807 0.4032607 0.1265202 -0.6571378 -0.3368566 1.4403603 0.4613683 -0.2299640 -1.76062685
b2 0.5039652 0.5675289 0.9722775 0.3007879 0.6155315 0.2737179 0.1235783 0.8146537 0.03849019
[,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81]
b 1.04426533 0.9849334 -0.1993729 -2.1915377 0.05498726 -0.8045629 0.6537419 2.5520945 -0.2593338
b2 0.05904346 0.4528220 0.2965003 0.1784881 0.26012509 0.4886883 0.8686121 0.2085445 0.2158082
[,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90]
b 1.2334733 -1.1134502 0.08112593 -0.6967268 0.6010776 -0.7342181 2.082550 -1.0448205 0.7554273
b2 0.4854508 0.3095164 0.72723613 0.4736194 0.8429309 0.2441740 0.848973 0.9982215 0.4190068
[,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99]
b 0.21755891 0.7289925 -0.2852289 0.7926916 -0.1922797 2.9059874 0.91663557 1.289271 -0.4278123
b2 0.06038115 0.5278188 0.1289604 0.9636656 0.9559447 0.1158746 0.04146398 0.471627 0.6225130
[,100]
b 0.3522587
b2 0.1516277
d <- rbind(a,b) # matrix; horizontal concatination
rownames(e) <- NULL # para poner nombre a renglones y cols de una matriz
Error in rownames(e) <- NULL : object 'e' not found
if (!require("dplyr")) install.packages("dplyr") # To install a package if is not unstalled
library(openxlsx) # on package
library(dplyr,here) # multiples packages
dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges:
-Groups of rows: -summarise() reduces multiple values down to a single summary; collapses a group into a single row.
city = "Davis"
#### Load data ####
#data <- read.csv(here("data/data_ww_cases.csv"))
data <- read.csv("data/data_ww_cases.csv")
unique(data$City)
head(data,10)
tail(city_ww,5)
city_ww = data %>% filter(City == city & SampleDate >= "2021-07-02" & SampleDate <= "2022-06-01")
city_ww = city_ww %>% select(SampleDate,N_gene, PMMoV,Testing,positives_crude)
head(city_ww)
city_ww = city_ww %>% rename(Positives = positives_crude)
city_ww = data %>% filter(City == city)%>% #& SampleDate >= date_ini & SampleDate <= date_end)
select(SampleDate,N_gene, PMMoV,Testing,positives_crude)%>%
rename(Positives = positives_crude)
city_ww = city_ww %>% mutate(PR = 100*Positives/Testing)
city_ww = city_ww %>% mutate(Positives = ifelse(is.na(Testing),NA,Positives))
head(city_ww)
write.csv(city_ww,"data/ww_cases.csv",row.names = F) # To save the data