0

Consider this data frame:

df <- data.frame(
id=c(1,2,3,2,2,1,1,4,5,3),
value=c("d1","d2","d3","d2","d2","d1","d1","d4","d5","d3"))

And this function:

f_dummy <- function(df){
    dt<-data.table(df)
    dt[, `:=` (value = ifelse(duplicated(value), NA, value)), by = id]
    return(dt)
}

By calling f_dummy(df), why nothing will be returned by function? While if you run the code (inside the function) in global environment, dt has the correct values.

dt<-data.table(df)
dt[, `:=` (value = ifelse(duplicated(value), NA, value)), by = id]

Output

# dt
    # id value
 # 1:  1    d1
 # 2:  2    d2
 # 3:  3    d3
 # 4:  2    NA
 # 5:  2    NA
 # 6:  1    NA
 # 7:  1    NA
 # 8:  4    d4
 # 9:  5    d5
# 10:  3    NA
989
  • 12,579
  • 5
  • 31
  • 53
  • 3
    It's an issue with printing. The object *is* returned; try `z = f_dummy(df); str(z)`. To get around this, I think the usual advice is to write `[]` at the end of any modification steps inside a function. – Frank Jun 16 '16 at 15:01
  • Yes you are right. But how can I get the same output back from function? – 989 Jun 16 '16 at 15:05
  • 1
    Here's the usual reference: https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html#why-do-i-have-to-type-dt-sometimes-twice-after-using-to-print-the-result-to-console I think adding `[]` at the end of the second line of your function should work. – Frank Jun 16 '16 at 15:08
  • @Frank Thanks. It works. – 989 Jun 16 '16 at 15:38

0 Answers0