76

I am very new to R and I am having trouble accessing a dataset I've imported. I'm using RStudio and used the Import Dataset function when importing my csv-file and pasted the line from the console-window to the source-window. The code looks as follows:

setwd("c:/kalle/R")
stuckey <- read.csv("C:/kalle/R/stuckey.csv")
point <- stuckey$PTS
time <- stuckey$MP

However, the data isn't integer or numeric as I am used to but factors so when I try to plot the variables I only get histograms, not the usual plot. When checking the data it seems to be in order, just that I'm unable to use it since it's in factor form.

smci
  • 32,567
  • 20
  • 113
  • 146
Joe
  • 983
  • 2
  • 7
  • 6
  • 1
    As Hong Ooi mentions, you probably have non-numeric values where you shouldn't. Try viewing the data (with, e.g., `View(stuckey)`) to see where the problem is. – Richie Cotton Mar 04 '11 at 09:55
  • Hi, I have a large file say 54000* 200 dimension data written in csv format. I have read the data in java file and haven't got any exception while converting to Double. But, while reading in R, i have encountered the same problem. I tried the methods posted here, but i am unsuccessful. PS: I am new to R – Rudra Murthy Jul 24 '14 at 13:46

8 Answers8

76

Both the data import function (here: read.csv()) as well as a global option offer you to say stringsAsFactors=FALSE which should fix this.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 20
    I don't think `stringsAsFactors` will help in this case, as all it does is control the conversion of character to factor. It doesn't influence whether read.csv imports a column as numeric or character, which is the underlying problem. – Hong Ooi Mar 04 '11 at 02:16
  • 7
    Moreover, `stringAsFactor = FALSE` generally forces the format to a character, which is exactly the opposite of what has to be achieved here. – gented Sep 14 '15 at 13:57
  • I don't recommend this solution because it really just converts to characters, absolutely pointless. – ABCD Jan 11 '16 at 12:25
  • Better to globally set the sensible default with `options('stringsAsFactors'=FALSE)`, then you can't forget. – smci Feb 22 '19 at 03:39
  • @gented, isn't having the values read in as a character at least more workable than as a factor? And what's the alternative? – James Mar 31 '19 at 05:42
  • The point is that `stringAsFactor = FALSE` doesn't solve the problem addressed in the question: if your data are numeric, then they must be converted to numeric and that's it (if this doesn't happen there must be another type of problem with the data, which `stringAsFactor` doesn't solve). – gented Mar 31 '19 at 11:57
  • 3
    Neither you nor I know that as the question came with no dataset to be actually _verifiable_. So if you downvoted based on that, you did it wrong. Anyway, I fail to see why people get so excited about _an eight year old answer_. We covered reading of data a bazillion other times, and sometimes even with a [mcve](https://stackoverflow.com/help/mcve). Without it, all we do is guessing. – Dirk Eddelbuettel Mar 31 '19 at 12:41
  • for me, "stringsAsFactors=FALSE" solved the issue that numeric data was imported as a factor. Now it is not - instead, yes, it is imported as "chr" but now I can convert it. Before that, it didn't work. – Ben Dec 18 '19 at 13:43
  • It's worth noting starting with R 4 this issue is obsolete as stringsAsFactors defaults to FALSE. – Krzysztof Mar 25 '22 at 16:22
29

By default, read.csv checks the first few rows of your data to see whether to treat each variable as numeric. If it finds non-numeric values, it assumes the variable is character data, and character variables are converted to factors.

It looks like the PTS and MP variables in your dataset contain non-numerics, which is why you're getting unexpected results. You can force these variables to numeric with

point <- as.numeric(as.character(point))
time <- as.numeric(as.character(time))

But any values that can't be converted will become missing. (The R FAQ gives a slightly different method for factor -> numeric conversion but I can never remember what it is.)

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
23

You can set this globally for all read.csv/read.* commands with options(stringsAsFactors=F)

Then read the file as follows: my.tab <- read.table( "filename.csv", as.is=T )

smci
  • 32,567
  • 20
  • 113
  • 146
Sam
  • 7,922
  • 16
  • 47
  • 62
  • 8
    careful with cases: 'stringsAsFactors' not 'StringsAsFactors' – artdv Oct 16 '13 at 21:48
  • 1
    Or you can simply add the option to the function: `my.tab <- read.table("filename.csv", stringsAsFactors=F)` – user890739 Mar 09 '16 at 23:37
  • I like the options method because it works with other reads such as read_rds. – done_merson Jun 23 '17 at 00:19
  • I'd drop all mention of `read.delim()`, it's nothing more than a thin wrapper for `read.csv(... sep = "\t")`. Otherwise this answer is the best answer to this question. And the OP specifically used `read.csv()` (which is also just a a thin wrapper for `read.table(... sep=',')`) – smci Nov 27 '18 at 23:31
8

When importing csv data files the import command should reflect both the data seperation between each column (;) and the float-number seperator for your numeric values (for numerical variable = 2,5 this would be ",").

The command for importing a csv, therefore, has to be a bit more comprehensive with more commands:

    stuckey <- read.csv2("C:/kalle/R/stuckey.csv", header=TRUE, sep=";", dec=",")

This should import all variables as either integers or numeric.

Daniel
  • 97
  • 1
  • 6
4

None of these answers mention the colClasses argument which is another way to specify the variable classes in read.csv.

 stuckey <- read.csv("C:/kalle/R/stuckey.csv", colClasses = "numeric") # all variables to numeric

or you can specify which columns to convert:

stuckey <- read.csv("C:/kalle/R/stuckey.csv", colClasses = c("PTS" = "numeric", "MP" = "numeric") # specific columns to numeric

Note that if a variable can't be converted to numeric then it will be converted to factor as default which makes it more difficult to convert to number. Therefore, it can be advisable just to read all variables in as 'character' colClasses = "character" and then convert the specific columns to numeric once the csv is read in:

stuckey <- read.csv("C:/kalle/R/stuckey.csv", colClasses = "character")
point <- as.numeric(stuckey$PTS)
time <- as.numeric(stuckey$MP)
Braide
  • 155
  • 3
  • 3
  • 13
2

I'm new to R as well and faced the exact same problem. But then I looked at my data and noticed that it is being caused due to the fact that my csv file was using a comma separator (,) in all numeric columns (Ex: 1,233,444.56 instead of 1233444.56).

I removed the comma separator in my csv file and then reloaded into R. My data frame now recognises all columns as numbers.

I'm sure there's a way to handle this within the read.csv function itself.

Rishal
  • 21
  • 1
1

This only worked right for me when including strip.white = TRUE in the read.csv command.

(I found the solution here.)

Helen
  • 316
  • 3
  • 16
0

for me the solution was to include skip = 0 (number of rows to skip at the top of the file. Can be set >0)

mydata <- read.csv(file = "file.csv", header = TRUE, sep = ",", skip = 22)

Tymo
  • 259
  • 6
  • 14