Getting dataframe directly from JSON-file?

前端 未结 1 585
清酒与你
清酒与你 2021-01-03 12:36

First, let me thank everybody who contributes to Stackoverflow and R! I\'m one of those R-users who is not so good at programming, but bravely try to use it for work, so the

相关标签:
1条回答
  • 2021-01-03 12:46

    Looking at the structure of txtJson you see that all of the useful bits are in txtJson$result$data:

    > sapply( txtJson$result$data, unlist )
         [,1]               [,2]              
    [1,] "2011-05-01 17:00" "2011-05-01 17:15"
    [2,] "Total"            "Total"           
    [3,] "8051"             "8362"            
    [4,] "44"               "66"              
    > t(sapply( txtJson$result$data, unlist ))
         [,1]               [,2]    [,3]   [,4]
    [1,] "2011-05-01 17:00" "Total" "8051" "44"
    [2,] "2011-05-01 17:15" "Total" "8362" "66"
    > as.data.frame(t(sapply( txtJson$result$data, unlist )) )
                    V1    V2   V3 V4
    1 2011-05-01 17:00 Total 8051 44
    2 2011-05-01 17:15 Total 8362 66
    

    In the process of gettting these as unlisted vectors and then passing to 'as.data.frame' they are now all class 'factor', so there is probably additional effort to re-class() these values. You can instead use:

    data.frame(t(sapply( txtJson$result$data, unlist )) ,stringsAsFactors=FALSE)
    

    And they would all be 'character'

    As far as importing CSV files, read.table()'s colClasses argument will accept "POSIXlt" or "POSIXct" as known types. The rule I believe is that there must an as._ method available. Here's a minimal example:

    > read.table(textConnection("2011-05-01 17:00"), sep=",", colClasses="POSIXct")
                       V1
    1 2011-05-01 17:00:00
    
    0 讨论(0)
提交回复
热议问题