R 将字符列表转换为数据帧

R 将字符列表转换为数据帧,r,dataframe,numeric,rjson,R,Dataframe,Numeric,Rjson,我有一些JSON格式的数据,我正试图在R中使用。我的问题是我无法获得正确格式的数据 require(RJSONIO) json <- "[{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}]" example <- fromJSON(json) example <- do.call(rbind,example) example <- as.data.frame(example,stri

我有一些JSON格式的数据,我正试图在R中使用。我的问题是我无法获得正确格式的数据

require(RJSONIO)

json <- "[{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}]"
example <- fromJSON(json)

example <- do.call(rbind,example)
example <- as.data.frame(example,stringsAsFactors=FALSE)

> example
   ID VALUE
1 id1    15
2 id2    10
require(RJSONIO)

json我建议您使用
jsonlite
包,默认情况下,该包会将其转换为数据帧

jsonlite::fromJSON(json)

   ID VALUE
1 id1    15
2 id2    10

注意:
numeric
问题仍然存在,因为
json
没有编码数据类型。因此,您必须手动转换数字列。

我建议您使用
jsonlite
包,默认情况下,该包会将其转换为数据帧

jsonlite::fromJSON(json)

   ID VALUE
1 id1    15
2 id2    10

注意:
numeric
问题仍然存在,因为
json
没有编码数据类型。因此,您必须手动转换数字列。

read.table
使用
type.convert
将数据转换为适当的类型。您可以在读取JSON数据后执行与清理步骤相同的操作

sapply(example,class)
         # ID       VALUE 
# "character" "character" 
example[] <- lapply(example, type.convert, as.is = TRUE)
sapply(example, class)
         # ID       VALUE 
# "character"   "integer" 
sapply(示例,类)
#ID值
#“字符”“字符”

示例[]
read.table
使用
type.convert
将数据转换为适当的类型。您可以在读取JSON数据后执行与清理步骤相同的操作

sapply(example,class)
         # ID       VALUE 
# "character" "character" 
example[] <- lapply(example, type.convert, as.is = TRUE)
sapply(example, class)
         # ID       VALUE 
# "character"   "integer" 
sapply(示例,类)
#ID值
#“字符”“字符”

示例[]为了跟进Ramnath关于过渡到
jsonlite
的建议,我对这两种方法进行了一些基准测试:

##RJSONIO vs. jsonlite for a simple example

require(RJSONIO)
require(jsonlite)
require(microbenchmark)

json <- "{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}"
test <- rep(json,1000)
test <- paste(test,collapse=",")
test <- paste0("[",test,"]")

func1 <- function(x){
  temp <- jsonlite::fromJSON(x)
}

func2 <- function(x){
  temp <- RJSONIO::fromJSON(x)
  temp <- do.call(rbind,temp)
  temp <- as.data.frame(temp,stringsAsFactors=FALSE)
}

> microbenchmark(func1(test),func2(test))
Unit: milliseconds
       expr       min        lq    median        uq       max neval
func1(test) 204.05228 221.46047 233.93321 246.90815 341.95684   100
func2(test)  21.60289  22.36368  22.70935  23.75409  27.41851   100

至少在我的机器上,
rjson
只是稍微快一点,尽管我没有测试它与
RJSONIO
相比的伸缩性,这可能是Ramnath建议的性能提升的地方

为了跟进Ramnath关于过渡到
jsonlite
的建议,我对这两种方法进行了一些基准测试:

##RJSONIO vs. jsonlite for a simple example

require(RJSONIO)
require(jsonlite)
require(microbenchmark)

json <- "{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}"
test <- rep(json,1000)
test <- paste(test,collapse=",")
test <- paste0("[",test,"]")

func1 <- function(x){
  temp <- jsonlite::fromJSON(x)
}

func2 <- function(x){
  temp <- RJSONIO::fromJSON(x)
  temp <- do.call(rbind,temp)
  temp <- as.data.frame(temp,stringsAsFactors=FALSE)
}

> microbenchmark(func1(test),func2(test))
Unit: milliseconds
       expr       min        lq    median        uq       max neval
func1(test) 204.05228 221.46047 233.93321 246.90815 341.95684   100
func2(test)  21.60289  22.36368  22.70935  23.75409  27.41851   100

至少在我的机器上,
rjson
只是稍微快一点,尽管我没有测试它与
RJSONIO
相比的伸缩性,这可能是Ramnath建议的性能提升的地方

它是否正确处理数值?正如我在注释中指出的,
json
数据不包含任何类型信息。所以你必须自己做转换。为了跟进你的建议,我做了一些基准测试。我将结果作为答案发布,因为它们不适合发表评论。谢谢你的意见!如果你想提高速度,试试
rjson
包,它比
RJSONIO
快10倍左右。它能正确处理数值吗?正如我在笔记中指出的,
json
数据不携带任何类型信息。所以你必须自己做转换。为了跟进你的建议,我做了一些基准测试。我将结果作为答案发布,因为它们不适合发表评论。谢谢你的意见!如果您想提高速度,请尝试
rjson
包,它比
RJSONIO
快10倍左右。为什么/如何在
示例中添加括号
保持数据帧结构?非常好。添加括号调用
感谢您的后续操作!为什么/如何在
示例中添加括号
保持数据帧结构?非常好。添加括号调用
感谢您的后续操作!