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
66
votes
3 answers

Why do R objects not print in a function or a "for" loop?

I have an R matrix named ddd. When I enter this, everything works fine: i <- 1 shapiro.test(ddd[,y]) ad.test(ddd[,y]) stem(ddd[,y]) print(y) The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column. If I put…
Sal Leggio
  • 661
  • 1
  • 5
  • 3
66
votes
4 answers

Why is it not advisable to use attach() in R, and what should I use instead?

Let's assume that we have a data frame x which contains the columns job and income. Referring to the data in the frame normally requires the commands x$jobfor the data in the job column and x$income for the data in the income column. However, using…
SavedByJESUS
  • 3,262
  • 4
  • 32
  • 47
64
votes
1 answer

How and when should I use on.exit?

on.exit calls code when a function exits, but how and when should I use it?
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
63
votes
8 answers

How to subtract years?

I have a date in R, e.g.: dt = as.Date('2010/03/17') I would like to subtract 2 years from this date, without worrying about leap years and such issues, getting as.Date('2008-03-17'). How would I do that?
gt6989b
  • 4,125
  • 8
  • 46
  • 64
63
votes
10 answers

Create an ID (row number) column

I need to create a column with unique ID, basically add the row number as an own column. My current data frame looks like this: V1 V2 1 23 45 2 45 45 3 56 67 How to make it look like this: V1 V2 V3 1 23 45 2 45 45 3 56 67 ? Many…
user10745
  • 637
  • 1
  • 5
  • 8
62
votes
3 answers

How to debug "contrasts can be applied only to factors with 2 or more levels" error?

Here are all the variables I'm working with: str(ad.train) $ Date : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels "Adelaide","Brisbane Lions",..: 1 1 1…
Troy
  • 683
  • 1
  • 7
  • 8
60
votes
3 answers

Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code

I received one of these errors. Error: unexpected symbol in "" Error: unexpected input in "" Error: unexpected string constant in "" Error: unexpected numeric constant in "" Error: unexpected SPECIAL in…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
60
votes
8 answers

Export data from R to Excel

I am writing code to export database from R into Excel, I have been trying others code including: write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="\t") write.csv(ALBERTA1,":\ALBERTA1.csv") your_filename_in_R =…
ntamjo achille
  • 781
  • 1
  • 7
  • 10
58
votes
29 answers

What's the biggest R-gotcha you've run across?

Is there a certain R-gotcha that had you really surprised one day? I think we'd all gain from sharing these. Here's mine: in list indexing, my.list[[1]] is not my.list[1]. Learned this in the early days of R.
Vince
  • 7,608
  • 3
  • 41
  • 46
57
votes
7 answers

rank and order in R

i am having trouble understanding the difference between the R function rank and the R function order. they seem to produce the same output: > rank(c(10,30,20,50,40)) [1] 1 3 2 5 4 > order(c(10,30,20,50,40)) [1] 1 3 2 5 4 Could somebody shed some…
Alex
  • 19,533
  • 37
  • 126
  • 195
55
votes
4 answers

Convert date-time string to class Date

I have a data frame with a character column of date-times. When I use as.Date, most of my strings are parsed correctly, except for a few instances. The example below will hopefully show you what is going on. # my attempt to parse the string to Date…
Btibert3
  • 38,798
  • 44
  • 129
  • 168
55
votes
4 answers

Subset rows in a data frame based on a vector of values

I have two data sets that are supposed to be the same size but aren't. I need to trim the values from A that are not in B and vice versa in order to eliminate noise from a graph that's going into a report. (Don't worry, this data isn't being…
Zelbinian
  • 3,221
  • 5
  • 20
  • 23
54
votes
4 answers

Plot multiple columns on the same graph in R

I have the following data frame: A B C D Xax 0.451 0.333 0.034 0.173 0.22 0.491 0.270 0.033 0.207 0.34 0.389 0.249 0.084 0.271 0.54 0.425 0.819 0.077 0.281 0.34 0.457 0.429 …
ifreak
  • 1,726
  • 4
  • 27
  • 45
54
votes
8 answers

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

I have a dataframe in a wide format, with repeated measurements taken within different date ranges. In my example there are three different periods, all with their corresponding values. E.g. the first measurement (Value1) was measured in the period…
daj
  • 6,962
  • 9
  • 45
  • 79
53
votes
4 answers

How do I get a list of built-in data sets in R?

Can someone please help how to get the list of built-in data sets and their dependency packages?
mockash
  • 1,204
  • 5
  • 14
  • 26