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

unique names for data.table + lag for unbalanced panel #633

Closed
wants to merge 33 commits into from
Closed

unique names for data.table + lag for unbalanced panel #633

wants to merge 33 commits into from

Conversation

matthieugomez
Copy link

   library(dplyr)
   library(data.table)
   DT <- as.data.table(mtcars) 
   dt <- 6
   DT %>% filter(cyl==dt)

Solution is is only implemented for filter_.data.table and filter_.grouped_dt as I was not sure it was the right way to do it.

@hadley
Copy link
Member

hadley commented Sep 26, 2014

Oh hmmm, I think a better solution would be to use just a non-syntactic name like _DT

@matthieugomez
Copy link
Author

Ok. I changed it.

matthieugomez and others added 2 commits September 26, 2014 16:45
add match_by as an option for lead and lag
@matthieugomez
Copy link
Author

I've added an option along_with for the functions lead and lag, which can be used instead of order_by. While lag(var, 1, order_by = date) creates a lagged variable based on previous date, lag(var, 1, along_with = date) created a lagged variable based on date - 1.

This is an important command (at least in social sciences) and the current solution (left_join) is not as satisfying (wrt syntax and performance). Related discussions:
http://stackoverflow.com/questions/25649056/create-lagged-variable-in-unbalanced-panel-data-in-r
https://groups.google.com/forum/#!searchin/manipulatr/lag/manipulatr/xDZjSuexXE0/d5LxZTJ4DHgJ
http://r.789695.n4.nabble.com/lags-for-unbalanced-panel-data-td3341764.html
http://aidcomplexity.blogs.casa.ucl.ac.uk/2012/08/16/lagging-panel-data-in-r/

Update: n can now be a lubridate period, which gives a good syntax to work with dates

library(lubridate)
df <- data.frame(     
  id = c("1", "1", "1", "1"),
  date = mdy(c("03/01/1992", "04/03/1992", "07/15/1992", "08/21/1992")),
  value = c(4.1, 4.5, 3.3, 5.3)
)
df <- df %>% mutate(date = floor_date(date, "month"))
df %>% group_by(id) %>% mutate(lag(value, months(1), along_with = date)) 

@matthieugomez matthieugomez changed the title unique names in data.table manipulations unique names for data.table + lag for unbalanced panel Sep 28, 2014
@matthieugomez
Copy link
Author

Inplace = TRUE creates a variable equal to true in current version of dplyr. I corrected it and added the option in select.

@matthieugomez
Copy link
Author

added that filter() creates new data in vignette about memory

@hadley hadley added the feature a feature request or enhancement label Sep 30, 2014
@hadley hadley added this to the 0.3.1 milestone Sep 30, 2014
@hadley hadley self-assigned this Sep 30, 2014
@matthieugomez
Copy link
Author

add merge on different variable names (like data.frames) #694 . Moreover, copy data.table only if data.tables must be rekeyed or if variables must be renamed.

# Accept different names
# In future versions, the command copy below may be replaced by shallow copies.

# first check no duplicates (not accepted in merge.data.table)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to wait on shallow copies here

@hadley
Copy link
Member

hadley commented Nov 19, 2014

I think this is a good idea in principle, but I've started adding a better abstraction - take a look at the new dt_subset()

@lock
Copy link

lock bot commented Jan 19, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants