Split and transform a R character string into numerical vector

▼魔方 西西 提交于 2019-12-12 03:18:05

问题


I want to convert the following json and put the values into a data frame. It almost works but as.data.frame() puts everything into one row.

require(rjson)
require(RCurl)

y = getURI(url1)
y
[1] "[{\"close\":5.45836392962902,\"highest\":5.45837200714172,\"lowest\":5.45836392962902,\"open\":5.45837200714172,\"start_time\":\"2012-01-29T18:29:24-08:00\"},{\"close\":5.45837200714172,\"highest\":5.45837200714172,\"lowest\":5.45834791002201,\"open\":5.45835598753471,\"start_time\":\"2012-01-29T18:28:24-08:00\"}]"

x = fromJSON(y)
> str(x)
List of 2
 $ :List of 5
  ..$ close     : num 5.46
  ..$ highest   : num 5.46
  ..$ lowest    : num 5.46
  ..$ open      : num 5.46
  ..$ start_time: chr "2012-01-29T18:29:24-08:00"
 $ :List of 5
  ..$ close     : num 5.46
  ..$ highest   : num 5.46
  ..$ lowest    : num 5.46
  ..$ open      : num 5.46
  ..$ start_time: chr "2012-01-29T18:28:24-08:00"

as.data.frame(x)
     close  highest   lowest     open                start_time  close.1 highest.1 lowest.1   open.1              start_time.1
1 5.458364 5.458372 5.458364 5.458372 2012-01-29T18:29:24-08:00 5.458372  5.458372 5.458348 5.458356 2012-01-29T18:28:24-08:00

Instead of it being on one row. I want them in two rows.

   close    highest   lowest     open                start_time  
1 5.458364  5.458372 5.458364 5.458372 2012-01-29T18:29:24-08:00 
2 5.458372  5.458372 5.458348 5.458356 2012-01-29T18:28:24-08:00

Is there something I can specify in as.data.table for this to work?

EDIT:

do.call(rbind,lapply(x,as.data.frame))

The above was able to coerce it into a data frame, but the time stamp column has two factors. This next part has its own question here

y = do.call(rbind,lapply(x,as.data.frame))
str(x)
'data.frame':   2 obs. of  5 variables:
 $ close     : num  5.46 5.46
 $ highest   : num  5.47 5.46
 $ lowest    : num  5.46 5.46
 $ open      : num  5.46 5.46
 $ start_time: Factor w/ 2 levels "2012-01-29T21:48:24-05:00",..: 1 2

If I try to convert the POSIX format I get

 x$start_time = as.POSIXct(x$start_time)
 x$start_time
[1] "2012-01-29 CST" "2012-01-29 CST"

But it loses the time data.


回答1:


You might try:

do.call(rbind,lapply(x,as.data.frame))


来源:https://stackoverflow.com/questions/9058728/split-and-transform-a-r-character-string-into-numerical-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!