128 Answered Questions for the topic R

R

10/18/19

Adding a column to a data.frame?

I have the data.frame below. I want to add a column that classifies my data according to column 1 (`h_no`) in that way that the first series of h_no 1,2,3,4 is class 1, the second series of `h_no`... more
R

10/17/19

Reorder levels of a factor without changing order of values?

I have data frame with some numerical variables and some categorical `factor` variables. The order of levels for those factors is not the way I want them to be. numbers <- 1:4 letters... more
R

10/17/19

Set default CRAN mirror permanent in R?

How can I set a specific CRAN mirror permanently in R?I want to set it permanently in my laptop so that when I do `install.packages()`, it won't ask me again which mirror to choose.
R

10/17/19

How do I change the formatting of numbers on an axis with ggplot?

I'm using R and ggplot to draw a scatterplot of some data, all is fine except that the numbers on the y-axis are coming out with computer style exponent formatting, i.e. 4e+05, 5e+05, etc. This is... more
R

10/16/19

What's the difference between `1L` and `1`?

I often seen the symbol `1L` (or `2L`, `3L`, etc) appear in R code. Whats the difference between `1L` and `1`? `1==1L` evaluates to `TRUE`. Why is `1L` used in R code?
R

10/15/19

Sample random rows in dataframe?

I am struggling to find the appropriate function that would return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?
R

10/13/19

How do I make a function?

R

10/12/19

To find whether a column exists in data frame or not?

I have a data.frame with the name "abcframe" a b c 1 1 1 2 2 3How might I find whether a column exists or not in a given data frame?For example, I would like to find... more
R

10/12/19

How to force R to use a specified factor level as reference in a regression?

How can I tell R to use a certain level as reference if I use binary explanatory variables in a regression?It's just using some level by default. lm(x ~ y + as.factor(b)) with `b {0, 1, 2, 3,... more
R

10/12/19

How to combine multiple conditions to subset a data-frame using "OR"?

I have a data.frame in R. I want to try two different conditions on two different columns, but I want these conditions to be inclusive. Therefore, I would like to use "OR" to combine the... more
R

10/11/19

Load multiple packages at once?

How can I load a bunch of packages at once with out retyping the require command over and over? I've tried three approaches all of which crash and burn.Basically, I want to supply a vector of... more
R

10/11/19

Replicate each row of data.frame and specify the number of replications for each row?

df <- data.frame(var1=c('a', 'b', 'c'), var2=c('d', 'e', 'f'), freq=1:3)What is the simplest way to expand the first two columns of the data.frame above, so thateach row appears the number of... more
R

10/10/19

Programmatically creating Markdown tables in R with KnitR?

I am just starting to learn about KnitR and the use of Markdown in generating R documents and reports. This looks to be perfect for a lot of the day to day reporting that I have to do with my job.... more
R

10/09/19

How to extract a substring in R according to a pattern?

Suppose I have a list of string: `string = c("G1:E001", "G2:E002", "G3:E003")`. Now I hope to get a vector of string that contains only the parts after the colon ":", i.e `substring =... more
R

10/08/19

ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"?

With this data frame ("df"): year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546I try to create a line chart like this: plot5 <- ggplot(df,... more
R

10/05/19

How do I calculate simple statistics like mean and median?

R

10/05/19

Correct way to specifiy optional arguments in R functions?

I am interested in what is the "correct" way to write functions with optional arguments in R.Over time, I stumbled upon a few pieces of code that take a different route here, and I couldn't find a... more
R

10/04/19

Define all functions in one .R file, call them from another .R file. How, if possible?

How do I call functions defined in abc.R file in another file, say xyz.R?A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line?
R

10/04/19

How to get a vertical geom_vline to an x-axis of class date?

Even though I found Hadley's post in the google group on `POSIXct` and `geom_vline`, I could not get it done. I have a time series from and would like to draw a vertical line for years 1998, 2005... more
R

10/04/19

Change value of variable with dplyr?

I regularly need to change the values of a variable based on the values on a different variable, like this: mtcars$mpg[mtcars$cyl == 4] <- NAI tried doing this with `dplyr` but failed... more
R

10/03/19

How do I write a command-line script?

R

10/03/19

How to avoid warning when introducing NAs by coercion?

I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using `as.numeric` to convert a character vector.For example: x <-... more
R

10/02/19

YAML current date in rmarkdown?

I'm wondering if there's a trick to put the current date in the YAML front-matter of a `.rmd` document to be processed by `knitr` and the `rmarkdown` package. I would like to take advantage of the... more
R

10/01/19

Read an Excel file directly from a R script?

How can I read an Excel file directly into R? Or should I first export the data to a text- or CSV file and import that file into R?
R

10/01/19

How can I create a correlation matrix in R?

I have 92 set of data of same type.I want to make a correlation matrix for any two combination possible.i.e. I want a matrix of 92 x92.such that element (ci,cj) should be correlation between ci and... more

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.