Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
d1 <- density(rnorm(100))
d2 <- density(rnorm(100))
plot(range(d1$x, d2$x), range(d1$y, d2$y), type = "n", xlab = "x",
ylab = "Density")
lines(d1, col = "red")
lines(d2, col = "blue")
pb <- txtProgressBar(style = 3, min = 0, max = length(files_list))
i <- 0
for (file in files_list){
i <- i+1
setTxtProgressBar(pb, i)
do_computations()
}
@nassimhaddad
nassimhaddad / MCA_extended.R
Last active December 20, 2015 10:59
an extended version of FactoMineR's MCA()
# install.packages("devtools")
# library(devtools)
# source_gist(6119745)
#' Example:
#'
#' data(tea)
#' newtea = tea[, c("Tea", "How", "how", "sugar", "where", "always")]
#' test <- MCA(newtea)
#' test <- add_modified_rates(test)
@nassimhaddad
nassimhaddad / install_packages.R
Last active December 19, 2015 22:58
installs my preferred packages.this is pretty useful right after a new installation.
# first install devtools in R
# then run:
# install.packages("devtools")
# library(devtools)
# source_gist(6031243)
lapply(c("ggplot2",
"xtable",
"markdown",
"gdata",
@nassimhaddad
nassimhaddad / ftp.R
Created July 10, 2013 13:34
Example of how to get data from an ftp site using R
library(RCurl)
# code in the address
address <- 'ftp://user:[email protected]'
# get the list of files
items <- strsplit(getURL(address, .opts=curlOptions(ftplistonly=TRUE)), "\r\n")[[1]]
# download a file
download.file(paste0(address, "/",items[1]) , items[1])
@nassimhaddad
nassimhaddad / mca.R
Created July 9, 2013 20:07
MCA in R, including many important variables
require(FactoMineR)
# load data tea
data(tea)
# select these columns
newtea = tea[, c("Tea", "How", "how", "sugar", "where", "always")]
mca1 = MCA(newtea, graph = FALSE, ncp = 5)
# coordinates of the categories of the variables along the dimensions
y_kl <- mca1$var$coord

Base Commands

Command Description
wc word count
ls list directory contents
cd change directory
pwd returns present working directory
type tells whether a command is built-in or a program
rm remove
##' @title f function
##'
##' f function
##'
##' @param x data
##' @param y data
##' @return data
##'
@nassimhaddad
nassimhaddad / tryCatch.R
Created May 6, 2013 08:45
using trycatch
result = tryCatch({
    expr
}, warning = function(w) {
    warning-handler-code
}, error = function(e) {
    error-handler-code
}, finally {
    cleanup-code
}

How to change language setting of R on Windows

  • Go to the "etc" folder under R program files folder.
  • Locate Rconsole file.
  • Find the line "language = ", change it to "language = en" if you want to run R in English.
  • Save the modified file in another folder first, delete the original one and place the new in place.