I have a full sample from 1986-1995 each year and 1997-2015 every other year. And it looks like:
I also have a variable lists for each year and it looks like:
My purpose is to subset the whole dataset into subsample datasets for each year by using their variable names in variable lists (2nd picture).
My attempt is as following:
name1986 <- var_name_list$X1986
name1987 <- var_name_list$X1987
name1988 <- var_name_list$X1988
name1989 <- var_name_list$X1989
name1990 <- var_name_list$X1990
name1991 <- var_name_list$X1991
name1992 <- var_name_list$X1992
name1993 <- var_name_list$X1993
name1994 <- var_name_list$X1994
name1995 <- var_name_list$X1995
name1996 <- var_name_list$X1996
name1997 <- var_name_list$X1997
name1999 <- var_name_list$X1999
name2001 <- var_name_list$X2001
name2003 <- var_name_list$X2003
name2005 <- var_name_list$X2005
name2007 <- var_name_list$X2007
name2009 <- var_name_list$X2009
name2011 <- var_name_list$X2011
name2013 <- var_name_list$X2013
name2015 <- var_name_list$X2015
And it works well:
However I got stuck in the following:
seq_yr = c(seq(1986,1996),seq(1997,2015,by=2))
for (Number in seq_yr){
dataname <- sprintf("name",Number)
file<- subset(data,select = c(ID,which(colnames(data) %in% dataname)))
# assign value to the variable name x
assign(x=str_c("data.",Number),value=file,envir=.GlobalEnv)
}
This successfully gives me a few subsample datasets but each of them only includes one variable. And this is what shows in my workspace:
Could you please let me know what went wrong with my code?
I would like to attach the part of my data in the following link:
https://drive.google.com/open?id=19KMl6eNsCEfcRJxWGoyWJSb34kPhmJaW
data4 is the data and header4 is the part of variable lists.
Thanks a lot!!! Please comment if you have any concerns regarding my question.
