Package ‘tibble’ in R

What is ‘tibble’ package?

According to Hadley Wickham “Tibbles are a modern reimagining of the data.frame, keeping what time has proven to be effective, and throwing out what is not.

The name comes from dplyr: originally you created these objects with tbl_df(), which was most easily pronounced as “tibble diff”. “

Find its similarities and dissimilarities with data.frame. More info here : tibble

Data manipulation in R using dplyr and plyr – for non programmers

#essential functions
library(‘dplyr’)
library(‘plyr’)

# creating dummy dataset
X1<-rnorm(n=50,mean = 30,sd=5)
X2<-factor(rbinom(n=50,size=1,p=0.7),labels=c(‘male’,’female’))
X3<-rnorm(n=50,mean=130, sd=15)
X4<-factor(rbinom(n=50,size=2,p=0.5),labels = c(‘M’,’C’,’I’))
dat<-data.frame(X1,X2,X3,X4)

#examine data frame

str(dat)
head(dat)

#create new column/variable
mutate(dat,sqX3=X3+10) #add new columns for X3*X3

#select rows
dat_filter<-filter(dat,X2 ==’male’) #focus on a subset of rows
dat_filter

#select columns
dat_col<-select(dat,X1,X2) #focus on a subset of variables
dat_col

#recode categorical var
dat$race<-mapvalues(dat$X4,from=c(‘M’,’C’,’I’), to=c(‘M’,’NM’,’NM’))
dat$race
dat$X4
#or
dat$race2[dat$X4==’M’]<-‘malay’
dat$race2[dat$X4==’C’ | dat$X4==’I’]<-‘Nonmalay’
dat$race2
dat$race2<-factor(dat$race2)
str(dat$race2)

#recoding numerical var
dat$highBP<-‘no’
dat$highBP
dat$highBP[dat$X3>129]<-‘yes’
dat$highBP<-factor(dat$highBP)
dat$highBP
dat$X3

#sort data
dat_sort<-arrange(dat,X2,X4) #re-order the rows
dat_sort2<-arrange(dat,desc(X2))
dat_sort2

#source: http://www.cookbook-r.com/Manipulating_data/Recoding_data/
#source: https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html