Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutate shouldn't make copies #191

Closed
hadley opened this issue Jan 20, 2014 · 5 comments
Closed

Mutate shouldn't make copies #191

hadley opened this issue Jan 20, 2014 · 5 comments
Assignees
Labels
Milestone

Comments

@hadley
Copy link
Member

@hadley hadley commented Jan 20, 2014

dfloc(iris)
dfloc(mutate(iris, area = Sepal.Length * Sepal.Width))
@ghost ghost assigned romainfrancois Jan 20, 2014
@hadley
Copy link
Member Author

@hadley hadley commented Jan 20, 2014

Or with new changes() function:

changes(mutate(iris, area = Sepal.Length * Sepal.Width), iris)

Should show only new area variable. Row names attribute should also be unchanged (not so important).

@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Jan 21, 2014

Actually it is tbl_df which makes a copy:

> changes( iris, tbl_df(iris))
Changed variables:
             old         new
Sepal.Length 0x1062f4e00 0x10ba9dc00
Sepal.Width  0x1062b2400 0x10ba3c000
Petal.Length 0x104b24600 0x10675a600
Petal.Width  0x104a92000 0x106749600
Species      0x102f6f4a0 0x102f84980

Changed attributes:
             old         new
names        0x100a400c8 0x100a88408
row.names    0x102f84420 0x102f84c00

mutate does not:

> tdf <- tbl_df(iris)
> changes( tdf, mutate(tdf, area = Sepal.Length * Sepal.Width) )
Changed variables:
          old new
area          0x1030d3e00

Changed attributes:
          old         new
names     0x10828f740 0x108918460
row.names 0x102e38150 0x102e06d00
class     0x108a9a098 0x108a9ca90

Presumably the class<- is responsible:

> tbl_df
function(data) {
  assert_that(is.data.frame(data))
  if (is.grouped_df(data)) return(ungroup(data))

  class(data) <- c("tbl_df", "tbl", "data.frame")
  data
}

@hadley
Copy link
Member Author

@hadley hadley commented Jan 21, 2014

Ooops, I'll have a go at implementing a better version of tbl_df tomorrow.

On Monday, January 20, 2014, Romain François notifications@github.com
wrote:

Actually it is tbl_df which makes a copy:

changes( iris, tbl_df(iris))
Changed variables:
old new
Sepal.Length 0x1062f4e00 0x10ba9dc00
Sepal.Width 0x1062b2400 0x10ba3c000
Petal.Length 0x104b24600 0x10675a600
Petal.Width 0x104a92000 0x106749600
Species 0x102f6f4a0 0x102f84980

Changed attributes:
old new
names 0x100a400c8 0x100a88408
row.names 0x102f84420 0x102f84c00

mutate does not:

tdf <- tbl_df(iris)
changes( tdf, mutate(tdf, area = Sepal.Length * Sepal.Width) )
Changed variables:
old new
area 0x1030d3e00

Changed attributes:
old new
names 0x10828f740 0x108918460
row.names 0x102e38150 0x102e06d00
class 0x108a9a098 0x108a9ca90

Presumably the class<- is responsible:

tbl_df
function(data) {
assert_that(is.data.frame(data))
if (is.grouped_df(data)) return(ungroup(data))

class(data) <- c("tbl_df", "tbl", "data.frame")
data
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/191#issuecomment-32810383
.

http://had.co.nz/

@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Jan 21, 2014

It is on its way. Essentially using the same trick as we used for grouped_df, using shallow_copy internally.

@hadley
Copy link
Member Author

@hadley hadley commented Jan 21, 2014

Definitively done in 3f7756d

@hadley hadley closed this Jan 21, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jun 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants