128 Answered Questions for the topic R

R

09/09/19

How to reorder data.table columns (without copying)?

I'd like to reorder columns in my `data.table` `x`, given a character vector of column names, `neworder`: library(data.table) x <- data.table(a = 1:3, b = 3:1, c = runif(3)) neworder... more
R

09/07/19

Paste multiple columns together?

I have a bunch of columns in a dataframe which I want to paste together (seperated by "-") as follows: data <- data.frame('a' = 1:3, 'b' = c('a','b','c'), ... more
R

09/06/19

How to create an empty R vector to add new items?

I want to use R in Python, as provided by the module Rpy2. I notice that R has very convenient `[]` operations by which you can extract the specific columns or lines. How could I achieve such a... more
R

09/05/19

Drop data frame columns by name?

I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like: df$x <- NULLBut I was hoping to do this with fewer... more
R

09/01/19

How to delete the first row of a dataframe in R?

I have a dataset with 11 columns with over a 1000 rows each. The columns were labeled V1, V2, V11, etc..I replaced the names with something more useful to me using the "c" command.I didn't realize... more
R

09/01/19

How do I save my plots to a PDF file?

R

08/24/19

How to increase font size in a plot in R?

I am confused. What is the right way to increase font size of text in the title, labels and other places of a plot? For example x <- rnorm(100) hist(x, xlim=range(x), xlab= "Variable... more
R

08/20/19

Opposite of %in%?

A categorical variable V1 in a data frame D1 can have values represented by the letters from A to Z. I want to create a subset D2, which excludes some values, say, B, N and T. Basically, I want a... more
R

08/15/19

How to substract a column by row?

I want to do an easy subtract in R, but I don't know how to solve it. I would like to know if I have to do a loop or if there is a function. I have a column with numeric variables, and I would... more
R

08/09/19

Remove an entire column from a data.frame in R?

Does anyone know how to remove an entire column from a data.frame in R? For example if I am given this data.frame: > head(data) chr genome region 1 chr1 hg19_refGene CDS ... more
R

08/04/19

How to succinctly write a formula with many variables from a data frame?

Suppose I have a response variable and a data containing three covariates (as a toy example): y = c(1,4,6) d = data.frame(x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2))I want to fit a linear... more
R

07/30/19

How do I read data into R?

R

07/16/19

Scheduling R Script?

I have written an R script that pulls some data from a database, performs several operations on it and post the output to a new database. I would like this script to run every day at a specific... more
R

07/14/19

Importing Excel files into R, xlsx or xls?

What is the best way to import an excel 2007 (.xlsx) file into R? I keep receiving this error: Error in .jnew("java/io/FileInputStream", file) : java.io.FileNotFoundException:... more
R

07/11/19

How can I put my text, code, and results all in one document?

R

07/06/19

Mean per group in a data.frame?

I have a `data.frame` and I need to calculate the mean per group (i.e. per `Month`, below). Name Month Rate1 Rate2 Aira 1 12 23 Aira 2 18 73 ... more
R

06/29/19

Prevent row names to be written to file when using write.csv?

Commands: t <- data.frame(v = 5:1, v2 = 9:5) write.csv(t, "t.csv")Resulting file: # "","v","v2" # "1",5,9 # "2",4,8 # "3",3,7 # "4",2,6 # "5",1,5How do I prevent first... more
R

06/25/19

How to drop columns by name in a data frame?

I have a large data set and I would like to read specific columns or drop all the others. data <- read.dta("file.dta")I select the columns that I'm not interested in: var.out <-... more
R

06/23/19

How to import multiple .csv files at once?

Suppose we have a folder containing multiple data.csv files, each containing the same number of variables but each from different times.Is there a way in R to import them all simultaneously rather... more
R

06/22/19

Extracting specific columns from a data frame?

I have an R data frame with 6 columns, and I want to create a new dataframe that only has three of the columns.Assuming my data frame is `df`, and I want to extract columns `A`, `B`, and `E`, this... more
R

06/21/19

How do I change the background color of a plot made with ggplot2?

By default, ggplot2 produces plots with a grey background. How do I change the color of the background of the plot?For example, a plot produced by the following code: library(ggplot2) ... more
R

06/19/19

How to sum a variable by group?

Let's say I have two columns of data. The first contains categories such as "First", "Second", "Third", etc. The second has numbers which represent the number of times I saw "First".For example: ... more
R

06/17/19

pull out p-values and r-squared from a linear regression?

How do you pull out the p-value (for the significance of the coefficient of the single explanatory variable being non-zero) and R-squared value from a simple linear regression model? For example...... more
R

06/15/19

How do I clear only a few specific objects from the workspace?

I would like to remove some data from the workspace. I know the "Clear All" button will remove all data. However, I would like to remove just certain data.For example, I have these data frames in... 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.