Tag: r

61 votes
30 answers
1.4K views
Is floating point math broken?

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004

avatar
Dwain
0 1 0 9
asked 07 September, 2021
59 votes
13 answers
1.9K views
How to change legend title in ggplot

I have the following plot like below. It was created with this command: library(ggplot2) df <- data.fram

avatar
Boyd
0 1 0 9
asked 07 September, 2021
54 votes
4 answers
2.4K views
Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x

avatar
Basil
0 1 0 9
asked 07 September, 2021
54 votes
6 answers
2.5K views
How to find the length of a string in R

How to find the length of a string (number of characters in a string) without splitting it in R? I know how to

avatar
Nereida
0 1 0 9
asked 07 September, 2021
52 votes
18 answers
1.6K views
Convert data.frame columns from factors to characters

I have a data frame. Let's call him bob: > head(bob) phenotype

avatar
Karolyn
0 1 0 9
asked 07 September, 2021
60 votes
15 answers
1.8K views
How can I trim leading and trailing white space?

I am having some troubles with leading and trailing white space in a data.frame. For example, I like to take a

avatar
Daniel
0 1 0 9
asked 07 September, 2021
62 votes
11 answers
3.1K views
Sample random rows in dataframe

I am struggling to find the appropriate function that would return a specified number of rows picked up random

avatar
Luis
0 1 0 9
asked 07 September, 2021
52 votes
2 answers
1.9K views
How to set limits for axes in ggplot2 R plots?

I plot the following: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cu

avatar
Roger
0 1 0 9
asked 07 September, 2021
57 votes
13 answers
1.9K views
Side-by-side plots with ggplot2

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(

avatar
Omar
0 1 0 9
asked 07 September, 2021
53 votes
13 answers
1.7K views
Write lines of text to a file in R

In the R scripting language, how do I write lines of text, e.g. the following two lines Hello World to a fi

avatar
Santiago
0 1 0 9
asked 07 September, 2021
57 votes
20 answers
2.7K views
How to rename a single column in a data.frame?

I know if I have a data frame with more than 1 column, I can use colnames(x) <- c("col1","col2") to ren

r
avatar
Boyd
0 1 0 9
asked 07 September, 2021
64 votes
29 answers
2.6K views
Elegant way to check for missing packages and install them?

I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and

avatar
Dwain
0 1 0 9
asked 07 September, 2021
56 votes
8 answers
926 views
How to add leading zeros?

I have a set of data which looks something like this: anim <- c(25499,25500,25501,25502,25503,25504) sex

avatar
Chaya
0 1 0 9
asked 07 September, 2021
65 votes
9 answers
1.6K views
Combine a list of data frames into one data frame by row

I have code that at one place ends up with a list of data frames which I really want to convert to a single bi

avatar
Emilia
0 1 0 9
asked 07 September, 2021
54 votes
12 answers
1.9K views
How can two strings be concatenated?

How can I concatenate (merge, combine) two values? For example I have: tmp = cbind("GAD", "AB") tmp # [,

avatar
Alphonso
0 1 0 9
asked 07 September, 2021
57 votes
2 answers
2.5K views
Why is `[` better than `subset`?

When I need to filter a data.frame, i.e., extract rows that meet certain conditions, I prefer to use the subse

avatar
Lawanda
0 1 0 9
asked 07 September, 2021
61 votes
6 answers
1.9K views
How do I install an R package from source?

A friend sent me along this great tutorial on webscraping NYtimes with R. I would really love to try it. Howev

avatar
Louetta
0 1 0 9
asked 07 September, 2021
53 votes
10 answers
1.8K views
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

avatar
Fabian
0 1 0 9
asked 07 September, 2021
52 votes
5 answers
2.7K views
How to write trycatch in R

I want to write trycatch code to deal with error in downloading from the web. url <- c( "http://stat.e

avatar
Brianne
0 1 0 9
asked 07 September, 2021
57 votes
15 answers
1.5K views
How to sum a variable by group

I have a data frame with two columns. First column contains categories such as "First", "Second", "Third", and

avatar
Emilia
0 1 0 9
asked 07 September, 2021