Add regression line equation and R^2 on graph

Here is one solution # GET EQUATION AND R-SQUARED AS STRING # SOURCE: https://groups.google.com/forum/#!topic/ggplot2/1TgH-kG5XMA lm_eqn <- function(df){ m <- lm(y ~ x, df); eq <- substitute(italic(y) == a + b %.% italic(x)*”,”~~italic(r)^2~”=”~r2, list(a = format(unname(coef(m)[1]), digits = 2), b = format(unname(coef(m)[2]), digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 <- p … Read more

Why are these numbers not equal?

General (language agnostic) reason Since not all numbers can be represented exactly in IEEE floating point arithmetic (the standard that almost all computers use to represent decimal numbers and do math with them), you will not always get what you expected. This is especially true because some values which are simple, finite decimals (such as … Read more

How to import multiple .csv files at once?

Something like the following should result in each data frame as a separate element in a single list: temp = list.files(pattern=”*.csv”) myfiles = lapply(temp, read.delim) This assumes that you have those CSVs in a single directory–your current working directory–and that all of them have the lower-case extension .csv. If you then want to combine those … Read more

Simultaneously merge multiple data.frames in a list

Another question asked specifically how to perform multiple left joins using dplyr in R . The question was marked as a duplicate of this one so I answer here, using the 3 sample data frames below: x <- data.frame(i = c(“a”,”b”,”c”), j = 1:3, stringsAsFactors=FALSE) y <- data.frame(i = c(“b”,”c”,”d”), k = 4:6, stringsAsFactors=FALSE) z … Read more

Formatting Decimal places in R

Background: Some answers suggested on this page (e.g., signif, options(digits=…)) do not guarantee that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of “significant figures”. However, in many domains … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)