Convert categorical variables to numeric in R

You can use unclass() to display numeric values of factor variables : To do so on all categorical variables, you can use sapply() : EDIT : A5C1D2H2I1M1N2O1R2T1’s solution works in one step : It only works if your data.frame doesn’t contain any character variable though (otherwise, they’ll be put to NA).

Categories R

Nested ifelse statement

If you are using any spreadsheet application there is a basic function if() with syntax: Syntax is exactly the same for ifelse() in R: The only difference to if() in spreadsheet application is that R ifelse() is vectorized (takes vectors as input and return vector on output). Consider the following comparison of formulas in spreadsheet application and in R for an example where … Read more

Categories R

“installation of package ‘FILE_PATH’ had non-zero exit status” in R

The .zip file provided by the authors is not a valid R package, and they do state that the source is for “direct use” in R (by which I assume they mean it’s necessary to load the included functions manually). The non-zero exit status simply indicates that there was an error during the installation of the “package”. … Read more

Categories R

Singularity in backsolve at level 0, block 1 in LME model

tl;dr as @HongOoi is telling you, wait and Adhesive are confounded in your model. lme is a little stupider/more stubborn than many of the other modeling functions in R, which will either warn you explicitly that you have confounded fixed effects or automatically drop some of them for you. It’s a bit easier to see this if you plot the data: This … Read more

Categories R

duplicate ‘row.names’ are not allowed error

Then tell read.table not to use row.names: and now your rows will simply be numbered. Also look at read.csv which is a wrapper for read.table which already sets the sep=’,’ and header=TRUE arguments so that your call simplifies to

Categories R

Rotating and spacing axis labels in ggplot2

Change the last line to By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead: The image above is from this blog post.

Categories R

How to set limits for axes in ggplot2 R plots?

Basically you have two options or Where the first removes all data points outside the given range and the second only adjusts the visible area. In most cases you would not see the difference, but if you fit anything to the data it would probably change the fitted values. You can also use the shorthand … Read more

Categories R

How to move or position a legend in ggplot2

In versions > 0.9.3 (when opts was deprecated) Older version: Unfortunately it’s a bug in ggplot2 which I really really hope to fix this summer. Update: The bug involving opts(legend.position = “left”) has been fixed using the most current version of ggplot2. In addition, version 0.9.0 saw the introduction of guide_legend and guide_colorbar which allow much finer control over the appearance and positioning of … Read more

Categories R

How to assign colors to categorical variables in ggplot2 that have stable mapping?

For simple situations like the exact example in the OP, I agree that Thierry’s answer is the best. However, I think it’s useful to point out another approach that becomes easier when you’re trying to maintain consistent color schemes across multiple data frames that are not all obtained by subsetting a single large data frame. Managing the … Read more

Categories R