Kirzilla
Kirzilla

Reputation: 16606

R is unexpectedly transforming field from CSV file to NA

I'm trying to parse CSV file in R. Here is the first line of CSV file with separator ~. Please note i literal at second field position.

2015-10-29 18:49:42~i~186.37.108.44~Mozilla/5.0 (Linux; Android 4.1.2; GT-S6810E Build/JZO54K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.94 Mobile Safari/537.36~ea01627ed45116787d3b1c0224a44d77~?~CL~1443~219~729~335~3155~9214~5

Here is how I'm trying to parse it:

> parsed <- read.csv('i.csv', header=F, sep='~')
> parsed$V2
  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [37] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [73] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[109] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[145] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[181] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[217] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[253] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[289] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> table(count.fields('i.csv', sep='~'))

 14 
310 

Why this happens? Why field#2 is NA istead of i? All other fields are ok, field#1 and field#3 do not contains i literal. All other fields are also OK.

> df$V1[1]
[1] 2015-10-29 18:38:04
257 Levels: 2015-10-29 18:38:04 2015-10-29 18:38:07 2015-10-29 18:38:12 ... 2015-10-29 18:51:46
> df$V3[1]
[1] 24.237.158.3
270 Levels: 1.144.97.1 1.187.195.221 1.187.204.84 1.39.12.184 1.39.13.227 1.39.137.12 1.39.33.86 ... 97.44.1.207

Upvotes: 3

Views: 86

Answers (1)

narendra-choudhary
narendra-choudhary

Reputation: 4826

For the sake of completion, I'm adding my comment as answer.

Almost all the read functions in R (read.csv, read.csv2, data, read.fwf, unzip, read.delim) call read.table function internally.

And read.table calls type.convert to recycle colClasses if it weren't provided with the function call.

From type.convert at R docs, it says

This is principally a helper function for read.table. Given a character vector, it attempts to convert it to logical, integer, numeric or complex, and failing that converts it to factor unless as.is = TRUE. The first type that can accept all the non-missing values is chosen.

So, type.convert checks if the value is logical, integer, real or complex, in this specific order and if all these options are ruled out, converts value to factor (or character if as.is=T).

In R-3.2.1, (buggy) implementation of strtoc and possibly typeconvert resulted in conversion of i to NA. strtoc has been corrected in R-3.3.0.

In R-3.3.0, type.convert('n±ki') return complex only if k ≠ 1.

From Changes in R-3.3.0:

type.convert("i") now returns a factor instead of a complex value with zero real part and missing imaginary part.

Upvotes: 1

Related Questions