[R] Using the output of strsplit

jim holtman jholtman at gmail.com
Tue Jan 19 20:23:13 CET 2010


'x' is a matrix and not a dataframe.  You should be doing

colnames(x) <- c("Date", "quarter")
x[,"Date"] <- as.Date(x[,"Date"])


It would help if you took a look at the structure you were using to
understand how to access.  'names' applied to a vector would give you
the output for 13000 more entries.  Think about it.

On Tue, Jan 19, 2010 at 2:07 PM, James Rome <jamesrome at gmail.com> wrote:
> 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 at gmail.com
>> <mailto: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 <mailto: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.
>>
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list