Imported a CSV-Dataset to R But the Values Becomes Factors

Importing csv file into R - numeric values read as characters

Whatever algebra you are doing in Excel to create the new column could probably be done more effectively in R.

Please try the following: Read the raw file (before any excel manipulation) into R using read.csv(... stringsAsFactors=FALSE). [If that does not work, please take a look at ?read.table (which read.csv wraps), however there may be some other underlying issue].

For example:

   delim = ","  # or is it "\t" ?
dec = "." # or is it "," ?
myDataFrame <- read.csv("path/to/file.csv", header=TRUE, sep=delim, dec=dec, stringsAsFactors=FALSE)

Then, let's say your numeric columns is column 4

   myDataFrame[, 4]  <- as.numeric(myDataFrame[, 4])  # you can also refer to the column by "itsName"


Lastly, if you need any help with accomplishing in R the same tasks that you've done in Excel, there are plenty of folks here who would be happy to help you out

warning in importing a CSV to R; - not meaningful for factors

You can do this by using the <- operator, which is the standard assignment operator in R.

dates <- read.csv(file = "dates.csv", header = FALSE)

instead of the one you are typing above.

You could also read this for the different assignment operators used in R.

Why does R print 'levels' of my csv-file, and how do I get rid of it?

This is a common problem while reading in text from .csv files. The standard base function converts all text to factors by default (here is why it made sense at some point). I know of at least three (short and easy) solutions:

dummyText <- read.csv("dummyText.csv", sep = ";", stringsAsFactors = FALSE)

dummyText <- readr::read_csv2("dummyText.csv")

dummyText <- rio::import("dummyText.csv")

Personally, I favour rio, since the import function has very sensible defaults for most data types (it can import many formats such as xlsx, STATA and SPSS files). But this is just a very personal opinion.

Problems importing csv file/converting from integer to double in R

See the help for read.csv: ?read.csv. Here is the relevant section:

colClasses: character.  A vector of classes to be assumed for the
columns. Recycled as necessary, or if the character vector
is named, unspecified values are taken to be ‘NA’.

Possible values are ‘NA’ (the default, when ‘type.convert’ is
used), ‘"NULL"’ (when the column is skipped), one of the
atomic vector classes (logical, integer, numeric, complex,
character, raw), or ‘"factor"’, ‘"Date"’ or ‘"POSIXct"’.
Otherwise there needs to be an ‘as’ method (from package
‘methods’) for conversion from ‘"character"’ to the specified
formal class.

Note that ‘colClasses’ is specified per column (not per
variable) and so includes the column of row names (if any).

Good luck with your quest to learn R. It's difficult, but so much fun after you get past the first few stages (which I admit do take some time).

try this and fix the others accordingly:

ex <- read.csv("exampleshort.csv",header=TRUE,colClasses=c("integer","integer","factor","integer","numeric","factor","factor","integer","numeric","numeric","numeric","numeric"), na.strings=c("."))

As BenBolker points out, the colClasses argument is probably not needed. However, note that using the colClasses argument can make the operation faster, especially with a large dataset.

na.strings must be specified. See the following section in ?read.csv:

 na.strings: a character vector of strings which are to be interpreted
as ‘NA’ values. Blank fields are also considered to be
missing values in logical, integer, numeric and complex
fields.

For reference purposes (this should not be used as the solution because the best solution is to import the data correctly in one step):
RET was not imported as an integer. It was imported as a factor. For future reference, if you want to convert a factor to a numeric, use

new_RET <-as.numeric(as.character(ex$RET))

NA Values Appear for All Data in Imported .csv File

The most likely issue is that the file is being imported as all text rather than as numeric data. If all of the data is numeric you can just use colClasses="numeric" as an argument to the read.csv() function and that should import correctly. You could also change the data class once it is in R, or give colClasses a vector of different classes if you have a variety of different data types (logical, character, numeric etc.) in your file.

Edit
Seeing as colClasses is not working (it is hard to say why without looking at your data), you can try this:

MyDF<-data.frame(sapply(MyDF,FUN=as.numeric))

Where MyDF is your datafraome. That will change all of your columns to numeric. If you have some charcter/factor/logical values in there this may not work as expected. You might want to check your excel file/csv to see why it is importing a NA column. It could be that there is a cell with a space in it that is being pulled in and this is throwing things off. You could always try deleting that empty column and retrying your import.



Related Topics



Leave a reply



Submit