GithubHelp home page GithubHelp logo

useful-r-functions's Introduction

Generic R functions

Priceless advice

What They Forgot to Teach You About R 

  • Nominare i file evitando spazi, accenti e puntuazione. Usa “-” per la human redability e “_” per isolare i metadata dal nome (es. "2020-02-10_sample1_WB") e identificarli facilmente con regex:
metadata <- stringr::str_split_fixed(x, [_\\.]
  • Crea Rprojects: avviando un notebook da progetto sei sicuro che la wd sia settata nella cartella di appartenenza e che l’ambiente delle variabili sia quello giusto. Non usare mai patwhay assoluti, soprattutto mai usare setwd(). Lascia che sia l’Rproj a richiamare la WD

  • Riavviare R prima di ogni esecuzione perchè:

    • potresti star usando funzioni omonime di librerie precedentemente caricate

    • un altro progetto potrebbe averti portato nella WD sbagliata

  • lasciare settato options(stringsAsFactors = FALSE)

  • Carica la libreria tidyverse per ultima, in modo da evitare che altre librerie mascherino le sue funzioni

Basic R

define a function

myfunction <- function(arg1, arg2, ... ){                  
  # statements  
  return(object)  
}

match operator

x %in% c("value1", "value2", "value3")

opposite of match operator

'%!in%' <- function(x,y)!('%in%'(x,y))

Magrittr

Pipe operator

f(x) # is equivalent to
x %>% f

f(x, parameters) # is equivalent to
x %>% f(parameters) 

h(g(f(x))) # is equivalent to
x %>% f %>% g %>% h 

f(y, x) # is equivalent to
x %>% f(y, .)

f(y, data = x) # is equivalent to
x %>% f(y, data = .)

f(g(x))
x %>% {f(g(.))}

Compound assigmnent

data$some_variable <- data$some_variable %>% transform 
# is equivalent to
data$some_variable %<>% transform

Data exploration

summary(iris)
skimr::skim(iris)

Dplyr

Matches at least with one element of the list

3 %in% c(1,2,3,4,5,6,7,8)  
[1] TRUE

Select rows by criteria

flights[flights$month == 1 & flights$day == 1, ]
# is equivalent to
flights %>% filter(month == 1, day == 1)
# multiple arguments are equivalent to &
flights %>% filter(month == 1 & day == 1)

Select only certains columns

# select a single column

flights %>% select(tailnum)

# select multiple column

flights %>% select(tailnum, day, month)

# select and rename the column

flights %>% select(tail_num = tailnum)

# select all but some columns

flights %>% select(-(year:day)) # or
flights %>% select(-c(year, month))

# select column based on theire name
flights %>% select(starts_with(c("one", "th")))

Rename a column

flights %>% rename(tail_num = tailnum)

Move a colon to the first position

df <- df %>% select(fisrt_column, second_column, everything())

Create a new column with formula

mutate(flights,  
 gain = arr_delay - dep_delay,  
 gain_per_hour = gain / (air_time / 60)  
)
# use the function transmutate() if you want to remove the used columns

Replace a single value by criterion

df <- df %>% mutate(height = replace(height, name ==Mike”, NA))

Summarize

summarise(flights,  
  delay.mean = mean(dep_delay, na.rm = TRUE), delay.sd = sd(dep_delay, na.rm = TRUE)  
)

tidyr

gather() - wide to long format

spread() - long to wide format

transpose a dataframe

df <- df %>%
  rownames_to_column() %>%
  gather(var, value, -rowname) %>% 
  spread(rowname, value) 

Favorite structures

Rename a specific column

# rename the second column
colnames(data.frame)[2] <- "newname2"

Select all the rows that contain a certain match inside a specified column

# selects all the rows containing a certain match inside a certain col
data.frame %>% filter(str_detect(col, fixed("match")))

Split the string inside a column into multiple column

# will split a string like "CTRL-2day LowGlucose"
data.frame %>% separate(col, # the column to split  
 c("col1", "col2", "col3"), # names of the columns  
 sep = "[:punct:]|[:blank:]", # separator  
 remove = TRUE, # remove the original column  
)

Change Factor levels

data.frame$col %<>% factor(levels = c("first", "second", "third"))

Calculate Standard Error

st.err <- function(x) {sd(x, na.rm=TRUE)/sqrt(length(x[!is.na(x)]))

ggplot2

Convert Y axis in percentage

scale_y_continuous(labels = function(x) paste0(x*100, "%"))

Rename the legend

+ scale_fill_x(name = 'Title')

Remove legend for one aesthetics

+  guides(fill = FALSE)
# or
+ scale_fill_x(guide = FALSE)

Declare the specific color for each factor level

myColors <- c("level1" = "#000000", "level2" = "#003300") 
# or
library(RColorBrewer)  
myColors  <- brewer.pal(length(levels(df$factors)), "Set1") 

names(myColors) <- levels(df$factors) 
+ scale_colour_manual(values = myColors)

Generate random data

set.seed(999)  
n = 1000  
df = data.frame(factors = sample(letters[1:8], n, replace = TRUE),  
x = rnorm(n), y = runif(n))

x <- runif(N)
y <- 5 * x + 3 + rnorm(N)

useful-r-functions's People

Contributors

ftucos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.