Skip to content
Prev 237584 / 398500 Next

Create single vector after looping through multiple data frames with GREP

Hi Simon,

The function below should do it or at least get you started...

getPlotData <- function (datalist, response, times)
{
  qdata <- sapply(datalist[times],
    function(df) {
      irow <- grepl(response, df$Response)
      df[irow, 2:5]
    }
  )

  # qdata is a matrix with rows Q1:Q4 and cols for times;
  # we turn it into a two col matrix with col 1 = time index
  # and col 2 = value
  time.index <- seq(4 * ncol(qdata))
  out <- cbind(time.index, as.numeric(qdata))
  rownames(out) <- paste(time.index, rownames(qdata), sep=".")
  colnames(out) <- c("time", response)
  out
}

#Example, get data for times 10:15 where Response contains "Economy"
x <- getPlotData(r, "Economy", 10:15)


Michael
On 11 October 2010 03:35, Simon Kiss <sjkiss at gmail.com> wrote: