## ----setup, include=FALSE--------------------------------------------- ## Global options knitr::opts_chunk$set(cache = TRUE) ## --------------------------------------------------------------------- a <- 10 b <- 3 / 10 d <- "star" e <- "10" f <- T|F g <- T|NA ## --------------------------------------------------------------------- is.integer(a) ; is.double(a) is.character(e) is.logical(f) ## --------------------------------------------------------------------- as.character(a) as.integer(e) as.numeric(f) as.numeric(d) ## --------------------------------------------------------------------- a==10L identical(a, 10L) identical(as.integer(a), 10L) ## --------------------------------------------------------------------- (v_num <- c(2, 3.6, 1e2)) (v_cha <- c("hi", "goodbye", "farewell")) ## --------------------------------------------------------------------- (v_le0 <- vector()) length(v_le0) (v_log <- vector(, length=2)) ## --------------------------------------------------------------------- v_mix_1 <- c(v_log, v_num) v_mix_2 <- c(v_num, v_cha) v_mix_3 <- c(v_log, v_cha) ## ----include=F-------------------------------------------------------- typeof(v_mix_1) ; typeof(v_mix_2 <- c(v_num, v_cha)) ; typeof(v_mix_3 <- c(v_log, v_cha)) ## --------------------------------------------------------------------- 1:10 ## --------------------------------------------------------------------- seq(1, 10) ## --------------------------------------------------------------------- seq(10, 1) ## --------------------------------------------------------------------- seq(1, 10, 2) seq(1, 10, length=5) seq(1, 10, 10) seq(1, 10, length=1) seq(1, 10, 2.5) seq(1, 10, le=2.5) # length is rounded up ## --------------------------------------------------------------------- rep(2, 5) rep(c(2,5), 2) rep(c(2, 5), c(2, 5)) ## --------------------------------------------------------------------- x <- c(1, 2, 5) x[1] x[1:2] x[c(1,3)] x[-2] x[-(1:2)] ## --------------------------------------------------------------------- y <- c(x, -1.2, 0, 10, 6.2, 3, -3, 9, 12, sqrt(2)) ## --------------------------------------------------------------------- x <- c(1:2,4,5,7,8,10,11) x[x<5 | x>7] ## --------------------------------------------------------------------- which(x<5 | x>7) ## --------------------------------------------------------------------- x <- 1:10 x*2 x > 5 ## --------------------------------------------------------------------- x <- rep(10,8) y <- c(1,2) x*y x+y ## --------------------------------------------------------------------- paste(1:3) # same as as.character(1:3) paste(1:3, 2) paste0(1:3, 2) paste(1:3, 2, sep=".") paste("day", 1:5) ## --------------------------------------------------------------------- x <- 0:7 # Are all the x values bigger than 0? all(x>0) # Are any of the x values negative? any(x<0) # then, it must be True that any(x>0) ## --------------------------------------------------------------------- x <- c(16.5, 16, 17, 12) length(x) sort(x) order(x) sort(x, decreasing=T) sum(x)/length(x) mean(x) min(x); max(x); range(x) ## --------------------------------------------------------------------- x <- c(1,0,5) str(x) attributes(x) x <- c(a=1, b=0, c=5) # or x <- c(1,0,5) ; names(x) <- c("a", "b", "c") # or setNames(c(1,0,5), c("a", "b", "c")) names(x) attributes(x) ## --------------------------------------------------------------------- x <- c(T, T, F, T) ## ----eval=F----------------------------------------------------------- # sum(x) ; mean(x) ## ----eval=F----------------------------------------------------------- # x[c(TRUE, TRUE, NA, FALSE)] ## --------------------------------------------------------------------- (x <- matrix(c(2,3,5,7,11,13), nrow=3)) (x <- matrix(c(2,3,5,7,11,13), ncol=3)) ## --------------------------------------------------------------------- (x <- scan("data/ex.data", skip = 1, quiet = TRUE)) (mx <- matrix(x, ncol=3, byrow=TRUE)) ## --------------------------------------------------------------------- nrow(mx) ; ncol(mx) ; dim(mx) ## --------------------------------------------------------------------- rownames(mx) <- paste0("r", 1:2) colnames(mx) <- c("a", "b", "c") mx ## --------------------------------------------------------------------- x <- 1:20 (ax <- array(x, dim=c(5,2,2))) dim(ax) ## --------------------------------------------------------------------- str(2:4) y <- matrix(2:4, nrow = 1) # row vector str(y) y <- matrix(2:4, ncol = 1) # column vector str(y) y <- array(2:4, 3) # 1d array str(y) ## --------------------------------------------------------------------- mx[2,1] # select the element in the cell (2,1) mx[2, ] # select the second row mx["r2", ] mx[ ,3] # select the third column mx[ ,"c"] ## --------------------------------------------------------------------- x <- matrix(1:16, ncol=4) (y <- x[c(1,4),c(3,4)]) ## --------------------------------------------------------------------- ax[3,2,1] ax[ ,2, ] ax[-1, ,1] ## ----include=F-------------------------------------------------------- a <- matrix(1:6, 2) a b <- a[,2] b is.vector(b) is.matrix(b) b <- as.matrix(b) b d <- array(1:12, c(2,3,2)) d length(d) d_copy <- d d_copy[,,1] <- b d ; d_copy ## --------------------------------------------------------------------- (x <- vector("list", length = 3)) ## --------------------------------------------------------------------- x1 <- 1:3 x2 <- c("A", "B", "C", "D", "E") x3 <- matrix(1:12, nrow=3) x[[1]] <- x1; x[[2]] <- x2; x[[3]] <- x3 x mylist <- list(x1, x2, x3) # define the list 'mylist' str(mylist) ## --------------------------------------------------------------------- (mylist2 <- list(comp1 = x1, comp2 = x2, comp3 = x3)) ## --------------------------------------------------------------------- names(mylist) names(mylist2) ## --------------------------------------------------------------------- newlist <- c(mylist, mylist2) is.list(newlist) str(newlist) ## --------------------------------------------------------------------- newlist <- list(mylist, mylist2) str(newlist) ## --------------------------------------------------------------------- mylist[1] mylist[[1]] mylist2$comp1 ## --------------------------------------------------------------------- mylist[[3]][,4] ## --------------------------------------------------------------------- dim(ax) dimnames(ax) dimnames(ax) <- list(letters[1:5], LETTERS[1:2], paste0("m", 1:2)) ax ax['a',,'m1'] ## ----echo=1:2--------------------------------------------------------- x <- list(list(1, 2), c(3, 4)) y <- c(list(1, 2), c(3, 4)) ## --------------------------------------------------------------------- b <- array(1:16, c(4, 2, 2)) ## ----include=F-------------------------------------------------------- str(x) ; str(y) x[[2]][2] y[[4]] dimnames(b) <- list(c("R1", "R2", "R3", "R4"), c("C1", "C2"), c("A", "B")) ## --------------------------------------------------------------------- country <- c("Italy","Germany","France","Germany","Germany","Germany", "France","Italy","Italy","France") ## --------------------------------------------------------------------- countryf <- factor(country) str(countryf) class(countryf) typeof(countryf) ## --------------------------------------------------------------------- countryf <- factor(country, levels=c("Italy", "France", "Germany")) str(countryf) ## --------------------------------------------------------------------- table(country) table(countryf) ## --------------------------------------------------------------------- age <- c(47,44,44,40,38,36,42,34,34,44) gender <- c(1,1,2,1,1,2,1,2,2,2) ## --------------------------------------------------------------------- tapply(age, countryf, mean) ## --------------------------------------------------------------------- genderf <- factor(gender) levels(genderf) <- c("F","M") str(genderf) ## --------------------------------------------------------------------- unclass(genderf) as.integer(genderf) ## ----eval=F----------------------------------------------------------- # sum(as.integer(c("1", "1", "0", "1", "1", "0"))) # sum(as.integer(fs)) # sum(as.integer(levels(fs)[fs])) ## --------------------------------------------------------------------- v1 <- factor(letters[1:5]) levels(v1) <- rev(levels(v1)) v2 <- factor(letters[1:5], levels = rev(letters[1:5])) ## ----include=F-------------------------------------------------------- x<-c(5, 12, 13, 12) y<-factor(x) str(y) unclass(y) ; as.integer(y) fs<-factor(c("1", "1", "0", "1", "1", "0")) fs str(fs) sum(as.integer(c("1", "1", "0", "1", "1", "0"))) sum(as.integer(fs)) sum(as.integer(levels(fs)[fs])) levels(fs)<-c("m", "f") table(fs) ## ----include=T-------------------------------------------------------- #What does this code do? How does v2 differ from v1? v1 <- factor(letters[1:5]) levels(v1) <- rev(levels(v1)) v1 str(v1) v2 <- factor(letters[1:5], levels = rev(letters[1:5])) v2 str(v2) ## --------------------------------------------------------------------- under40 <- age < 40 dat <- data.frame(country=countryf, age=age, sex=genderf, under40=under40) ## --------------------------------------------------------------------- str(dat) dplyr::glimpse(dat) class(dat) is.data.frame(dat) ## --------------------------------------------------------------------- head(dat) tail(dat) ## ----eval=F----------------------------------------------------------- # View(dat) # edit(dat) ## --------------------------------------------------------------------- dat[3,2] dat[1:3, 2:4] dat[3, ] ## --------------------------------------------------------------------- x <- dat[, 2] str(x) ## --------------------------------------------------------------------- dat[ , c("age", "sex")] ## --------------------------------------------------------------------- dat["age"] str(dat["age"]) ## --------------------------------------------------------------------- dat$sex ## --------------------------------------------------------------------- dat$under40 <- NULL head(dat) ## ----message=F-------------------------------------------------------- attach(dat) ## --------------------------------------------------------------------- age ## --------------------------------------------------------------------- detach(dat) ## --------------------------------------------------------------------- x <- runif(8) y <- letters[1:8] z <- sample(c(rep(T,5),rep(F,3))) ## ----include=F-------------------------------------------------------- newdf <- data.frame(x,y,z) head(newdf) data.frame(nome=c("Andrea", "Biagio", "Caterina", "Daria", "Enrico"), anni=as.integer(c(19, 35, 23, 45, 30))) ## --------------------------------------------------------------------- library(readxl) fav_food <- read_excel("data/favourite-food.xlsx") fav_food str(fav_food) ## ----include=F-------------------------------------------------------- str(as.data.frame(fav_food))