title: Reading, recoding, subsetting, and reshaping sequentially labeled data.frames in R
body: <p>I'm having a tough time adapting a script that I've previously used to read in and recode sequentially labeled data.tables.</p>
  
  <p>I have a series of data.tables in R that are sequentially labeled: <code>df1</code>,<code>df2</code>,<code>df3</code>, etc. There are then specific (and inconsistent) rules that I apply to create new variables in those <code>data.tables</code> called <code>status</code> and <code>csat</code>.</p>
  
  <p>What I would like to do is:</p>
  
  <ol>
  <li>Read in the data tables</li>
  <li>Recode the <code>csat</code> variable into a new variable</li>
  <li>Subset the data.table so it only includes 4 variables (<code>csat</code>,<code>csat_d</code>,<code>id</code>,and <code>status</code>)</li>
  <li>Merge the data.table with previous tables using an outer join (so it can be reshaped into long form)   </li>
  </ol>
  
  <p>I am trying to address points 1-3 in the script below, and have no idea how to implement #4. </p>
  
  <p><em>EDITED</em>: </p>
  
  <blockquote>
  <pre><code>df_names&lt;-c(df,df2,df3)  # Create list of data.tables
  csat_vars&lt;-c("CustomerId","csat","csat_d","status") # Create list of 4 variables
  
  out &lt;- lapply(1:length(df_names), function(idx) {
    d &lt;- df_names[idx]
    d$csat_d &lt;- recode(d$csat,"1:5=0;6:7=1;NA=NA;")
    d &lt;- subset(d, select=c(csat_vars))
  })
  </code></pre>
  </blockquote>
  
  <p>I am agnostic about whether or not it's better to use <code>data.table</code> or <code>data.frame</code> (these are small datasets), so any help is welcome.</p>
  
  <p>Mini-datasets here:</p>
  
  <pre><code>&gt; dput(head(df))
  structure(list(respid = c(1499L, 433L, 2600L, 2282L, 1503L, 3304L
  ), csat = c(4L, 6L, NA, NA, 6L, 4L), status = c("Active", "Active", 
  "Active", "Active", "Active", "Active"), touch = c(2L, 3L, 2L, 
  3L, 2L, 2L)), .Names = c("CustomerId", "csat", "status", "touch"), class = c("data.table", 
  "data.frame"), row.names = c(NA, -6L), .internal.selfref = &lt;pointer: 0x7f800301b778&gt;)
  
  &gt; dput(head(df2_r))
  structure(list(respid = c(6L, 5L, 149L, 147L, 270L, 145L), csat = c(4L, 
  NA, 6L, 7L, 7L, 4L), status = c("Active", "Lapsed/Churned", "Active", 
  "Active", "Active", "Active"), touch = c(3L, NA, 3L, 1L, 3L, 
  1L)), .Names = c("CustomerId", "csat", "status", "touch"), class = c("data.table", 
  "data.frame"), row.names = c(NA, -6L), .internal.selfref = &lt;pointer: 0x7f800301b778&gt;)
  
  &gt; dput(head(df3))
  structure(list(respid = c(1713L, 1611L, 1630L, 1773L, 1391L, 
  1571L), csat = c(4L, 6L, 4L, 5L, 7L, 4L), status = c("Active", 
  "Active", "Active", "Active", "Active", "Active"), AGENCY_1 = c(NA_integer_, 
  NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
  )), .Names = c("CustomerId", "csat", "status", "AGENCY_1"), class = c("data.table", 
  "data.frame"), row.names = c(NA, -6L), .internal.selfref = &lt;pointer: 0x7f800301b778&gt;)
  </code></pre>