How to rename a single column in a data.frame?

This is a generalized way in which you do not have to remember the exact location of the variable: # df = dataframe # old.var.name = The name you don’t like anymore # new.var.name = The name you want to get names(df)[names(df) == ‘old.var.name’] <- ‘new.var.name’ This code pretty much does the following: names(df) looks … Read more

Why is `[` better than `subset`?

This question was answered in well in the comments by @James, pointing to an excellent explanation by Hadley Wickham of the dangers of subset (and functions like it) [here]. Go read it! It’s a somewhat long read, so it may be helpful to record here the example that Hadley uses that most directly addresses the … Read more

How to add leading zeros?

The short version: use formatC or sprintf. The longer version: There are several functions available for formatting numbers, including adding leading zeroes. Which one is best depends upon what other formatting you want to do. The example from the question is quite easy since all the values have the same number of digits to begin … Read more

How do I install an R package from source?

If you have the file locally, then use install.packages() and set the repos=NULL: install.packages(path_to_file, repos = NULL, type=”source”) Where path_to_file would represent the full path and file name: On Windows it will look something like this: “C:\\RJSONIO_0.2-3.tar.gz”. On UNIX it will look like this: “/home/blah/RJSONIO_0.2-3.tar.gz”.

How to change legend title in ggplot

This should work: p <- ggplot(df, aes(x=rating, fill=cond)) + geom_density(alpha=.3) + xlab(“NEW RATING TITLE”) + ylab(“NEW DENSITY TITLE”) p <- p + guides(fill=guide_legend(title=”New Legend Title”)) (or alternatively) p + scale_fill_discrete(name = “New Legend Title”)

How can two strings be concatenated?

paste() is the way to go. As the previous posters pointed out, paste can do two things: concatenate values into one “string”, e.g. > paste(“Hello”, “world”, sep=” “) [1] “Hello world” where the argument sep specifies the character(s) to be used between the arguments to concatenate, or collapse character vectors > x <- c(“Hello”, “World”) … Read more

Changing column names of a data frame

Use the colnames() function: R> X <- data.frame(bad=1:3, worse=rnorm(3)) R> X bad worse 1 1 -2.440467 2 2 1.320113 3 3 -0.306639 R> colnames(X) <- c(“good”, “better”) R> X good better 1 1 -2.440467 2 2 1.320113 3 3 -0.306639 You can also subset: R> colnames(X)[2] <- “superduper”

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