I successfully combined my data frames, and am now on my next hurdle. I had combined the data and quarter, and used tapply to count the entries for each unique date/quarter pair. ar= tapply(ewrgnd$gw, list(ewrgnd$dq), sum) #for each date/quarter combination sums the gw (which are all 1) dq=row.names(ar) spl=strsplit(dq) But I need to split them back into the separate date and quarter. So I used strsplit(), and get> spl[[1]] [1] "2009-01-01" "60" [[2]] [1] "2009-01-01" "61" [[3]] [1] "2009-01-01" "62" [[4]] [1] "2009-01-01" "63" [[5]] [1] "2009-01-01" "68" . . . But lists throw me. I want to get separate vectors of the date and quarter out of my list. All the things I have seen extract rows from the list. I need to extract columns. Thanks list, Jim Rome
Tena koe Jim You could use unlist and matrix. For example:> tempList[[1]] [1] "a" "A" [[2]] [1] "b" "B" [[3]] [1] "c" "C"> unlist(tempList)[1] "a" "A" "b" "B" "c" "C"> matrix(unlist(tempList), nrow=3)[,1] [,2] [1,] "a" "B" [2,] "A" "c" [3,] "b" "C"> matrix(unlist(tempList), nrow=3, byrow=T)[,1] [,2] [1,] "a" "A" [2,] "b" "B" [3,] "c" "C"> matrix(unlist(tempList), nrow=3, byrow=T)[,1][1] "a" "b" "c" HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of James Rome > Sent: Tuesday, 19 January 2010 10:49 a.m. > To: r-help at r-project.org > Subject: [R] Using the output of strsplit > > I successfully combined my data frames, and am now on my next hurdle. > > I had combined the data and quarter, and used tapply to count > the entries for each unique date/quarter pair. > ar= tapply(ewrgnd$gw, list(ewrgnd$dq), sum) #for each date/quarter > combination sums the gw (which are all 1) > dq=row.names(ar) > spl=strsplit(dq) > But I need to split them back into the separate date and > quarter. So I used strsplit(), and get > > spl > [[1]] > [1] "2009-01-01" "60" > > [[2]] > [1] "2009-01-01" "61" > > [[3]] > [1] "2009-01-01" "62" > > [[4]] > [1] "2009-01-01" "63" > > [[5]] > [1] "2009-01-01" "68" > . . . > > But lists throw me. I want to get separate vectors of the > date and quarter out of my list. All the things I have seen > extract rows from the list. I need to extract columns. > > Thanks list, > Jim Rome > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Try this also: do.call(rbind, spl) On Mon, Jan 18, 2010 at 7:48 PM, James Rome <jamesrome at gmail.com> wrote:> I successfully combined my data frames, and am now on my next hurdle. > > I had combined the data and quarter, and used tapply to count the > entries for each unique date/quarter pair. > ar= tapply(ewrgnd$gw, list(ewrgnd$dq), sum) ? #for each date/quarter > combination sums the gw (which are all 1) > dq=row.names(ar) > spl=strsplit(dq) > But I need to split them back into the separate date and quarter. So I > used strsplit(), and get >> spl > [[1]] > [1] "2009-01-01" "60" > > [[2]] > [1] "2009-01-01" "61" > > [[3]] > [1] "2009-01-01" "62" > > [[4]] > [1] "2009-01-01" "63" > > [[5]] > [1] "2009-01-01" "68" > . . . > > But lists throw me. I want to get separate vectors of the date and > quarter out of my list. All the things I have seen extract rows from the > list. I need to extract columns. > > Thanks list, > Jim Rome > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
This suggestion does not work. x seems to have twice the number of entries as spl.> x <- do.call(rbind, spl) > names(x) <- c('Date', 'quarter') > x$Date <- as.Date(x$Date)Error in x$Date : $ operator is invalid for atomic vectors> x$quarter <- as.numeric(x$quarter)Error in x$quarter : $ operator is invalid for atomic vectors> names(x)[1] "Date" "quarter" NA NA NA NA NA [8] NA NA NA NA NA NA NA [15] NA NA NA NA NA NA NA [22] NA NA NA NA NA NA NA [29] NA NA NA NA NA NA NA # and on for 13000 entries! Making it a data.frame dod not work either. I also tried qt=c(length(spl)) dt=c(length(spl)) ### for(j in 1:length(spl)) { dt[j]=spl[[j]][1] qt[j]=spl[[j]][2] } qt=as.numeric(qt) dt=as.POSIXlt(dt) rate=as.vector(ar) ratedata=data.frame(c(rate=rate,date=dt,quarter=qt)) ### but then ratedata was totally wrong. Thanks, Jim On 1/18/10 4:59 PM, Dennis Murphy wrote:> Hi James: > > To slurp your list into a matrix, run > > x <- do.call(rbind, yourlistname) > > Date <- as.Date(x[, 1]) > quarter <- as.numeric(x[, 2]) > > You could also convert x to a data frame with as.data.frame(x) : > > x <- as.data.frame(x) > names(x) <- c('Date', 'quarter') > x$Date <- as.Date(x$Date) > x$quarter <- as.numeric(x$quarter) > > do.call() takes a function as its first argument and a list as its > second argument. > > HTH, > Dennis > > On Mon, Jan 18, 2010 at 1:48 PM, James Rome <jamesrome@gmail.com > <mailto:jamesrome@gmail.com>> wrote: > > I successfully combined my data frames, and am now on my next hurdle. > > I had combined the data and quarter, and used tapply to count the > entries for each unique date/quarter pair. > ar= tapply(ewrgnd$gw, list(ewrgnd$dq), sum) #for each date/quarter > combination sums the gw (which are all 1) > dq=row.names(ar) > spl=strsplit(dq) > But I need to split them back into the separate date and quarter. So I > used strsplit(), and get > > spl > [[1]] > [1] "2009-01-01" "60" > > [[2]] > [1] "2009-01-01" "61" > > [[3]] > [1] "2009-01-01" "62" > > [[4]] > [1] "2009-01-01" "63" > > [[5]] > [1] "2009-01-01" "68" > . . . > > But lists throw me. I want to get separate vectors of the date and > quarter out of my list. All the things I have seen extract rows > from the > list. I need to extract columns. > > Thanks list, > Jim Rome > > ______________________________________________ > R-help@r-project.org <mailto:R-help@r-project.org> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]