Articles

Affichage des articles du septembre, 2021

utf8 code

 http://www.ltg.ed.ac.uk/~richard/utf-8.cgi Choose Character, enter the characters to convert in the text box; Example •, will give you: Character • Character name BULLET Hex code point 2022 Decimal code point 8226 Hex UTF-8 bytes E2 80 A2  Octal UTF-8 bytes 342 200 242  UTF-8 bytes as Latin-1 characters bytes â <80> ¢ Then: x <- "\u2022" print(x) [1] "•"

Angle of line linking two points

 Let center.x and center.y be the center of the Cartesian plan. Let x1 and y1 be the coordinate of the point. The angle between (center.x, center.y) and (x1, y1) is: (atan((y1-center.y)/(x1-center.x))+pi*(as.numeric((x1-center.x)<0))) %% (2*pi) The 0 is at right of the center, pi is at left, pi/2 at the top and 3 pi/2 at the bottom.

Convert a matrix to vector and back

  You must use byrow = FALSE (the default) to convert the vector back to a matrix. > (d <- matrix(1:6, nrow=2, ncol=3))      [,1] [,2] [,3] [1,]    1    3    5 [2,]    2    4    6 > (v <- as.vector(d)) [1] 1 2 3 4 5 6 > matrix(v, nrow=2, ncol=3, byrow = FALSE)      [,1] [,2] [,3] [1,]    1    3    5 [2,]    2    4    6

An example of barplot with ggplot2

  From R-help discussion list; answer by Rui Barradas. I like this answer and I copy it here: h <- c(574,557,544,535,534,532,531,527,526,525,         524,520,518,512,507,504,504,489,488,488,         487,484,484,474,472,455,444,420)  nms <- c("Fribourg(f)","Valais(d)","Appenzell Rhodes Intérieures",           "Fribourg(d)","Jura","Schwyz","Schaffhouse","Berne(f)",           "Thurgovie","Valais(f)","Argovie","Appenzell Rhodes Extérieures",           "Genève","Zoug","Tessin","Neuchâtel","Vaud","Uri","Nidwald",           "Berne(d)","Zurich","Obwald","Saint-Gall","Soleure","Lucerne",           "Glaris","Bâle-Campagne","Bâle-Ville")  nms <- factor(nms, levels = nms)  library(ggplot2)  data.frame(height = h,

Check datasets in package with utf-8, latin1 or bytes characters

Here is a hack of tools:::.check_package_datasets() to show what dataset has utf-8, latin1 or bytes characters. pkgDir must be the path of the package.   check_package_datasets <- function (pkgDir)    {     oLC_ct <- Sys.getlocale("LC_CTYPE")     on.exit(Sys.setlocale("LC_CTYPE", oLC_ct))     Sys.setlocale("LC_CTYPE", "C")     oop <- options(warn = -1)     on.exit(options(oop), add = TRUE)     check_one <- function(x, ds) {       if (!length(x))          return()       if (is.list(x))          lapply(unclass(x), check_one, ds = ds)       if (is.character(x)) {         xx <- unclass(x)         enc <- Encoding(xx)         latin1 <<- latin1 + sum(enc == "latin1")         utf8 <<- utf8 + sum(enc == "UTF-8")         bytes <<- bytes + sum(enc == "bytes")         unk <- xx[enc == "unknown"]         ind <- .Call(tools:::C_check_nonASCII2, unk)         if (length(ind)) {