You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
record is lost if splitCols contains empty value #45
dt1<- fread("V1 V2 V3 x b;c;d 1 y d;ef 2 z d;ef 3 m tmp 4 n tmp 5")
dt1[4, V2:=''] # this record will be lost because V2 value is empty stringdt1[5, V2:=NA_character_] # NA value is processed correctly
cSplit(dt1, splitCols='V2', sep=';', direction='long')
@aushev , does cSplit(dt1, splitCols = 'V2', sep = ';', direction = 'long', stripWhite = FALSE) do what you expect?
cSplit(dt1, splitCols = "V2", sep = ";", direction = "long", stripWhite = FALSE)
## V1 V2 V3
## 1: x b 1
## 2: x c 1
## 3: x d 1
## 4: y d 2
## 5: y ef 2
## 6: z d 3
## 7: z ef 3
## 8: m 4
## 9: n NA 5
@aushev , sorry. I was testing with different data--where the empty cells are replaced with " ". In other words, the approach would work with:
cSplit(replace(dt1, dt1 == "", " "), splitCols = "V2", sep = ";", direction = "long", stripWhite = FALSE)
## V1 V2 V3
## 1: x b 1
## 2: x c 1
## 3: x d 1
## 4: y d 2
## 5: y ef 2
## 6: z d 3
## 7: z ef 3
## 8: m 4
## 9: n NA 5
That's a possible workaround.
This happens because the approach used when direction = "long" is essentially:
dt1[, list(unlist(strsplit(V2, ";"))), by = V1]
## V1 V1
## 1: x b
## 2: x c
## 3: x d
## 4: y d
## 5: y ef
## 6: z d
## 7: z ef
## 8: n NA
yes, in my case I came to the same workaround: I replace empty values before calling cSplit(). But I prefer to replace not with whitespace but with sep - in this case you don't need to add stripWhite = FALSE:
However, I still believe that current behaviour is not very good: without knowing about this issue, user can loose data. Maybe it's worth adding a warning, or at least add this info in the help section?
@aushev , Sure. If you would like to propose the edits to the documentation via a pull request, that would be appreciated.
I should note, however, that this behavior is consistent with other approaches, like unnest from "tidyr":
library(dplyr)
library(tidyr)
dt1 %>% mutate(V2 = strsplit(V2, ";")) %>% unnest(V2)
## Source: local data frame [8 x 3]
##
## V1 V3 V2
## (chr) (int) (chr)
## 1 x 1 b
## 2 x 1 c
## 3 x 1 d
## 4 y 2 d
## 5 y 2 ef
## 6 z 3 d
## 7 z 3 ef
## 8 n 5 NA
@aushev, @bilydr, I haven't had a chance to do any work on this, but the tidyr package (version 0.5.0) has a new function called separate_rows which does what you are looking for.
cSplit_fread(dt1, "V2", ";", "long")
## V1 V2 V3
## 1: x 1 1
## 2: x 2 1
## 3: x 3 1
## 4: y 1 2
## 5: y 3 2
## 6: z 2 3
## 7: z 4 3
## 8: m NA 4
## 9: n NA 5
Activity
mrdwab commentedon Jan 17, 2016
@aushev , does
cSplit(dt1, splitCols = 'V2', sep = ';', direction = 'long', stripWhite = FALSE)do what you expect?aushev commentedon Jan 17, 2016
@mrdwab, no, unfortunately it gives the same result
mrdwab commentedon Jan 17, 2016
@aushev , sorry. I was testing with different data--where the empty cells are replaced with
" ". In other words, the approach would work with:That's a possible workaround.
This happens because the approach used when
direction = "long"is essentially:aushev commentedon Jan 17, 2016
yes, in my case I came to the same workaround: I replace empty values before calling
cSplit(). But I prefer to replace not with whitespace but withsep- in this case you don't need to addstripWhite = FALSE:However, I still believe that current behaviour is not very good: without knowing about this issue, user can loose data. Maybe it's worth adding a warning, or at least add this info in the help section?
mrdwab commentedon Jan 17, 2016
@aushev , Sure. If you would like to propose the edits to the documentation via a pull request, that would be appreciated.
I should note, however, that this behavior is consistent with other approaches, like
unnestfrom "tidyr":bilydr commentedon Jun 10, 2016
encountered the same issue - it would be great to have an option of keeping rows with empty strings. thanks
mrdwab commentedon Jun 14, 2016
@aushev, @bilydr, I haven't had a chance to do any work on this, but the
tidyrpackage (version 0.5.0) has a new function calledseparate_rowswhich does what you are looking for.mrdwab commentedon Mar 8, 2018
Will be fixed in the forthcoming version of
cSplit:Note that all empty spaces are filled with
NA.