Skip to content

Commit 57aef24

Browse files
committed
implement reviewer suggestions
1 parent f53af10 commit 57aef24

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/convert.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ end
1818
namask(ri::RVector{Int32}) = [i == R_NA_INT32 for i in ri.data]
1919
namask(rn::RNumericVector) = map(isna_float64, reinterpret(UInt64, rn.data))
2020
# if re or im is NA, the whole complex number is NA
21-
# FIXME avoid temporary Vector{Bool}
2221
namask(rc::RComplexVector) = [isna_float64(v.re) || isna_float64(v.im) for v in reinterpret(Complex{UInt64}, rc.data)]
2322
namask(rv::RNullableVector) = rv.na
2423

@@ -38,12 +37,13 @@ function julia_vector(rl::RLogicalVector, force_nullable::Bool)
3837
end
3938

4039
# converts Vector{Int32} into Vector{R} replacing R_NA_INT32 with 0
41-
na2zero{R}(::Type{R}, v::Vector{Int32}) = [x != R_NA_INT32 ? R(x) : zero(R) for x in v]
40+
# it's assumed that v fits into R
41+
na2zero{R}(::Type{R}, v::Vector{Int32}) = [ifelse(x != R_NA_INT32, x % R, zero(R)) for x in v]
4242

43-
# convert to [Nullable]CategoricalArray{String} if `ri`is a factor,
43+
# convert to [Nullable]CategoricalArray{String} if `ri` is a factor,
4444
# or to [Nullable]Array{Int32} otherwise
4545
function julia_vector(ri::RIntegerVector, force_nullable::Bool)
46-
!isfactor(ri) && return _julia_vector(eltype(ri.data), ri, force_nullable) # not a factor
46+
isfactor(ri) || return _julia_vector(eltype(ri.data), ri, force_nullable)
4747

4848
# convert factor into [Nullable]CategoricalArray
4949
rlevels = getattr(ri, "levels", emptystrvec)
@@ -55,9 +55,11 @@ function julia_vector(ri::RIntegerVector, force_nullable::Bool)
5555
# FIXME set ordered flag
5656
refs = na2zero(REFTYPE, ri.data)
5757
pool = CategoricalPool{String, REFTYPE}(rlevels)
58-
(force_nullable || (findfirst(refs, zero(REFTYPE)) > 0)) ?
59-
NullableCategoricalArray{String, 1, REFTYPE}(refs, pool) :
60-
CategoricalArray{String, 1, REFTYPE}(refs, pool)
58+
if force_nullable || (findfirst(refs, zero(REFTYPE)) > 0)
59+
return NullableCategoricalArray{String, 1, REFTYPE}(refs, pool)
60+
else
61+
return CategoricalArray{String, 1, REFTYPE}(refs, pool)
62+
end
6163
end
6264

6365
function sexp2julia(rex::RSEXPREC)

0 commit comments

Comments
 (0)