r - Using vectors as inputs in new variables -
my data frame spdata, includes date, open price, closing price , volume s&p500 last 25 years (~ 250 days per year). additionally, have spdata$year
, vector year date column stored numerically, 1990 - 2015.
library(dplyr) spdata1990 <- filter(spdata, year == 1990)
results in data frame ~250 observations, 1 each trading day in 1990. did 25 years already.
is there way create formula save other data corresponding each year new data frame (spdata1991, spdata 1992, spdata1993, etc.)? trying think through for(i in years) loop corresponding formula, years <- unique(spdata$year, false)
, not familiar enough programming in general figure out.
thanks
with @user20650...
# reproducible example! set.seed(123) year_range = 1990:2014 spdata <- data.frame(year=sample(year_range,1000,replace=true), sales=runif(1000,min=100,max=200) ) # split list data frames on "spdatayyyy" , store in global environment list2env(split(spdata, paste0("spdata",spdata$year)), envir = .globalenv) ls() # [1] "spdata" "spdata1990" "spdata1991" "spdata1992" "spdata1993" "spdata1994" # [7] "spdata1995" "spdata1996" "spdata1997" "spdata1998" "spdata1999" "spdata2000" # [13] "spdata2001" "spdata2002" "spdata2003" "spdata2004" "spdata2005" "spdata2006" # [19] "spdata2007" "spdata2008" "spdata2009" "spdata2010" "spdata2011" "spdata2012" # [25] "spdata2013" "spdata2014" "year_range"
Comments
Post a Comment