In an attempt to save space I omitted zeros from my CSV files as a sort of sparse-ish representation (all data is numeric):
table = read.csv(text = "
V1,V2,V3
0.3,1.2,1.5
0.5,,2.1
,.1,")
Here's what I get:
> table
V1 V2 V3
1 0.3 1.2 1.5
2 0.5 NA 2.1
3 NA 0.1 NA
I can go ahead and change the NAs to 0:
table[is.na(table)] = 0
V1 V2 V3
1: 0.3 1.2 1.5
2: 0.5 0.0 2.1
3: 0.0 0.1 0.0
Just wondering if there's a one liner to do this while reading in, preferably with data.table's fread?:
table = fread("
V1,V2,V3
0.3,1.2,1.5
0.5,,2.1
,.1,")
More info: the reason I'd like to avoid
table[is.na(table)] = 0
is that while fread on my data is really fast, this operation is quite slow! (Not sure exactly why.) My dataset is 336 rows x 3939 columns. (G. Grothendieck's custom class answer is fast, thanks for that idea!)