Questions tagged [r-faq]

The r-faq tag is created to group a limited number of questions discussing problems that come up regularly on the R tag. It is not the official FAQ on R for SO, but should serve as an interesting source of information on common problems.

The tag is created to group a limited number of questions discussing problems that come up regularly on the tag. It is not the official FAQ on R for SO, but should serve as an interesting source of information on common problems.

The "real" original R-FAQ is reliably found at: https://cran.r-project.org/doc/FAQ/R-FAQ.html and is typically the first google hit for "r-faq".

251 questions
18
votes
2 answers

strptime, as.POSIXct and as.Date return unexpected NA

When I try to parse a timestamp in the following format: "Thu Nov 8 15:41:45 2012", only NA is returned. I am using Mac OS X, R 2.15.2 and Rstudio 0.97.237. The language of my OS is Dutch: I presume this has something to do with it. When I try…
Hemmik
  • 183
  • 1
  • 5
17
votes
4 answers

subset rows with (1) ALL and (2) ANY columns larger than a specific value

I have a data frame with an id column and some (potentially many) columns with values, here 'v1', 'v2': df <- data.frame(id = c(1:5), v1 = c(0,15,9,12,7), v2 = c(9,32,6,17,11)) # id v1 v2 # 1 1 0 9 # 2 2 15 32 # 3 3 9 6 # 4 4 12 17 # 5 5 …
Rock
  • 2,827
  • 8
  • 35
  • 47
17
votes
2 answers

Different legend-keys inside same legend in ggplot2

Let's say I don't need a 'proper' variable mapping but still would like to have legend keys to help the chart understanding. My actual data are similar to the following df df <- data.frame(id = 1:10, line = rnorm(10), points =…
Michele
  • 8,563
  • 6
  • 45
  • 72
17
votes
3 answers

apply() is slow - how to make it faster or what are my alternatives?

I have a quite large data frame, about 10 millions of rows. It has columns x and y, and what I want is to compute hypot <- function(x) {sqrt(x[1]^2 + x[2]^2)} for each row. Using apply it would take a lot of time (about 5 minutes, interpolating…
aplavin
  • 2,199
  • 5
  • 32
  • 53
17
votes
5 answers

Storing R Objects in a relational database

I frequently create nonparametric statistics (loess, kernel densities, etc) on data I pull out of a relational database. To make data management easier I would like to store R output back inside my DB. This is easy with simple data frames of numbers…
JD Long
  • 59,675
  • 58
  • 202
  • 294
17
votes
4 answers

Convert list to data frame while keeping list-element names

I have list where the elementnames are ID-tags and contains a vector with numeric values. These are of unequal(!) length. I want to transform it to a data frame where I have the ID in one column and the numeric values in another column. E.g.: …
ego_
  • 1,409
  • 6
  • 21
  • 31
16
votes
1 answer

Create a group index for values connected directly and indirectly

I would like to generate indices to group observations based on two columns. But I want groups to be made of observation that share, at least one observation in commons. In the data below, I want to check if values in 'G1' and 'G2' are connected…
Malta
  • 1,883
  • 3
  • 17
  • 30
16
votes
2 answers

Debugging unexpected errors in R -- how can I find where the error occurred?

Sometimes R throws me errors such as Error in if (ncol(x) != 2) { : argument is of length zero with no additional information, when I've written no such code. Is there a general way for finding which function in which package causes an…
Tim
  • 13,904
  • 10
  • 69
  • 101
15
votes
1 answer

How to order data by value within ggplot facets

I have the following data frame: library(tidyverse) tdat <- structure(list(term = c("Hepatic Fibrosis / Hepatic Stellate Cell Activation", "Cellular Effects of Sildenafil (Viagra)", "Epithelial Adherens Junction Signaling", "STAT3 Pathway",…
littleworth
  • 4,781
  • 6
  • 42
  • 76
14
votes
1 answer

Error in if/while (condition) : argument is not interpretable as logical

I received the error Error in if (condition) { : argument is not interpretable as logical or Error in while (condition) { : argument is not interpretable as logical What does it mean, and how do I prevent it?
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
14
votes
1 answer

How can I read the source code for an R function?

I have a data frame and I want to learn how the summary generates it's information. Specifically, how does summary generate a count for the number of elements in each level of a factor. I can use summary, but I want to learn how to work with…
Ed Fine
  • 717
  • 1
  • 6
  • 18
12
votes
1 answer

How should I deal with "'someFunction' is not an exported object from 'namespace:somePackage'" error?

I have this error: 'someFunction' is not an exported object from 'namespace:somePackage' Does anyone know how to solve it?
zx8754
  • 52,746
  • 12
  • 114
  • 209
12
votes
1 answer

Order of operator precedence when using ":" (the colon)

I am trying to extract values from a vector using numeric vectors expressed in two seemingly equivalent ways: x <- c(1,2,3) x[2:3] # [1] 2 3 x[1+1:3] # [1] 2 3 NA I am confused why the expression x[2:3] produces a result different from x[1+1:3]…
Marc
  • 767
  • 6
  • 18
11
votes
3 answers

acos(1) returns NaN for some values, not others

I have a list of latitude and longitude values, and I'm trying to find the distance between them. Using a standard great circle method, I need to find: acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(long2-long1)) And multiply this by the…
David Manheim
  • 2,553
  • 2
  • 27
  • 42
10
votes
3 answers

Create categorical variable in R based on range

I have a dataframe with a column of integers that I would like to use as a reference to make a new categorical variable. I want to divide the variable into three groups and set the ranges myself (ie 0-5, 6-10, etc). I tried cut but that divides…
Stedy
  • 7,359
  • 14
  • 57
  • 77
1 2 3
16
17