在R中跨许多CSV优化变量和年份合并代码

时间:2019-07-03 17:17:47

标签: r

经济学研究助理。我正在与R一起将College Scorecard和IPEDS数据集合并以输出到单个CSV,以便在Stata中运行OLS。我对College Scorecard合并代码感到非常满意,但是虽然IPEDS合并代码有效,但它非常繁琐,我很乐意帮助您对其进行优化。以下是其中一个变量的年份之一的合并代码的摘要。如果有助于查看数据集的结构,请告诉我,我将看到如何发布数据集。

# 1.2.1.1 - 2008
ipeds2008ControlTuitionSTFRAdmissions = read.csv(file = file.path(ipedsDir, "2008ControlTuitionSTFRAdmissions.csv"), stringsAsFactors = FALSE)
ipeds2008Gender = read.csv(file = file.path(ipedsDir, "2008Gender.csv"), stringsAsFactors = FALSE)
ipeds2008Grad = read.csv(file = file.path(ipedsDir, "2008Grad.csv"), stringsAsFactors = FALSE)
ipeds2008Race = read.csv(file = file.path(ipedsDir, "2008Race.csv"), stringsAsFactors = FALSE)

.
.
.
# 1.2.2.1 - 2008
ipeds2008GenderGrad = merge(ipeds2008Gender, 
                            ipeds2008Grad)
ipeds2008GenderGradRace = merge(ipeds2008GenderGrad, 
                                ipeds2008Race)
ipeds2008 = merge(ipeds2008GenderGradRace, 
                  ipeds2008ControlTuitionSTFRAdmissions)

# Garbage management.
rm(ipeds2008Gender, 
   ipeds2008ControlTuitionSTFRAdmissions, 
   ipeds2008GenderGrad,
   ipeds2008GenderGradRace,
   ipeds2008Race,
   ipeds2008Grad)
.
.
.
# Now, let's merge all the IPEDS years.
ipedsMerged = Reduce(function(x, y) merge(x, y, all=TRUE), list(ipeds2008, 
                                                                ipeds2009, 
                                                                ipeds2010, 
                                                                ipeds2011, 
                                                                ipeds2012,
                                                                ipeds2013,
                                                                ipeds2014,
                                                                ipeds2015,
                                                                ipeds2016,
                                                                ipeds2017))
.
.
.
# Now, let's merge all the IPEDS years.
ipedsMerged = Reduce(function(x, y) merge(x, y, all=TRUE), list(ipeds2008, 
                                                                ipeds2009, 
                                                                ipeds2010, 
                                                                ipeds2011, 
                                                                ipeds2012,
                                                                ipeds2013,
                                                                ipeds2014,
                                                                ipeds2015,
                                                                ipeds2016,
                                                                ipeds2017))

# Because we currently have each variable year as a separate variable (with nulls in observation years that
# aren't in that year), we want to merge all the variable years into one variable, if that makes sense. Dplyr's
# coalesce function is very good at that. Let's start with the public vs private variable.
ipedsMerged$institutionalControl = coalesce(ipedsMerged$IC2008.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2009.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2010.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2011.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2012.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2013.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2014.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2015.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2016.Institutional.control.or.affiliation,
                                            ipedsMerged$IC2017.Institutional.control.or.affiliation)

# Dropping all the rows that we don't need now.
ipedsMerged = ipedsMerged[ , !(names(ipedsMerged) %in% c("IC2008.Institutional.control.or.affiliation",
                                           "IC2009.Institutional.control.or.affiliation",
                                           "IC2010.Institutional.control.or.affiliation",
                                           "IC2011.Institutional.control.or.affiliation",
                                           "IC2012.Institutional.control.or.affiliation",
                                           "IC2013.Institutional.control.or.affiliation",
                                           "IC2014.Institutional.control.or.affiliation",
                                           "IC2015.Institutional.control.or.affiliation",
                                           "IC2016.Institutional.control.or.affiliation",
                                           "IC2017.Institutional.control.or.affiliation",
                                           "IDX_EF",
                                           "IDX_GR"))]
.
.
.
ipedsTbl = as_tibble(ipedsMerged)

# Let's do some more garbage management.
rm(ipeds2008,
   ipeds2009,
   ipeds2010,
   ipeds2011,
   ipeds2012,
   ipeds2013,
   ipeds2014,
   ipeds2015,
   ipeds2016,
   ipeds2017,
   ipedsMerged)


# 1.3 - For the piece de resistance, let's merge ipeds and college scorecard.
ipedsCS = left_join(ipedsTbl, csTbl, by = c("unitid" = "unitid", "year" = "year"))

write_csv(ipedsCS, path = file.path(thisDir, "Data", "Merged", "ipedsCS.csv"))

0 个答案:

没有答案