Tag: r-faq

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
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
929 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.6K 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
57 votes
30 answers
2.1K views
How to find the statistical mode?

In R, mean() and median() are standard functions which do what you'd expect. mode() tells you the intern

avatar
Corrin
0 1 0 9
asked 07 September, 2021
57 votes
20 answers
1.6K views
Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,5

avatar
Rudy
0 1 0 9
asked 07 September, 2021
58 votes
11 answers
2.7K views
Quickly reading very large tables as dataframes

I have very large tables (30 million rows) that I would like to load as a dataframes in R. read.table() has a

avatar
Thurman
0 1 0 9
asked 07 September, 2021
58 votes
17 answers
2.1K views
Create an empty data.frame

I'm trying to initialize a data.frame without any rows. Basically, I want to specify the data types for e

avatar
Keila
0 1 0 9
asked 07 September, 2021
59 votes
7 answers
2.1K views
Test if a vector contains a given element

How to check if a vector contains a given value?

avatar
Jesenia
0 1 0 9
asked 07 September, 2021
46 votes
15 answers
712 views
Drop unused factor levels in a subsetted data frame

I have a data frame containing a factor. When I create a subset of this dataframe using subset or another inde

avatar
Omar
0 1 0 9
asked 07 September, 2021
49 votes
12 answers
2.6K views
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

R provides two different methods for accessing the elements of a list or data.frame: [] and [[]]. What is the

avatar
Xavier
0 1 0 9
asked 07 September, 2021
57 votes
12 answers
2.6K views
How to unload a package without restarting R

I'd like to unload a package without having to restart R (mostly because restarting R as I try out differ

avatar
Vi
0 1 0 9
asked 07 September, 2021
57 votes
8 answers
2.2K views
What is the difference between require() and library()?

What is the difference between require() and library()?

avatar
Kurtis
0 1 0 9
asked 07 September, 2021