mutate. doesn't work?

test.dt <- data.table(
a = c(1,2),
b = c(4,5))

a b

1 2

4 5

test.dt %>%
mutate.(c = b * 2)

shows new "c" column

a b c

1 4 8

2 5 10

test.dt

same as original, does not show new "c" column. Why not?

test.dt$c

NULL

test.dt %>% # This works
.[, c := b * 2]

If you want to save the column c to test.dt you need to assign it to test.dt.

test.dt <- data.table(
a = c(1,2),
b = c(4,5))

test.dt <- test.dt %>%
mutate.(c = b * 2)

mutate() does not have a . after it.

However, I think you are getting your objects mixed up. Whilst you can operate on a data.table object using mutate() and other tidyverse functions, I'm not sure why you want to mix the methods.

I'd either use tibbles (or data frames) with tidyverse functions, stick with data.table syntax or use dtplyr to mix the two if you know what you are doing.

Thanks. I tried this and it works, but I don't understand. I thought mutate added the new column automatically.

Thanks for your reply but you are not up to date: see pkg (tidytable::mutate. ), with period. Much more efficient than mutate (without period). See reply from toryn_stat. Works fine.

Fair enough, but if you are going to use an obscure package then you really should state that in your question.

1 Like

To be honest, I didn't even notice the period... I agree with martin.R that the question isn't clear.

My fault for not mentioning that I was using the tidytable package which adds the period. I find the period commands much more efficient. My apology.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.