R fromJSON - $运算符对原子向量无效

时间:2017-07-08 21:47:02

标签: json r dataframe

为什么我在下面收到此错误:

Error: $ operator is invalid for atomic vectors

我的简单测试的虚拟代码:

l <- fromJSON('[{"winner":"68694999",  "votes":[
   {"ts":"Thu Mar 25 03:13:01 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}},
   {"ts":"Thu Mar 25 03:13:08 UTC 2010", "user":{"name":"Lamur","user_id":"68694999"}}],
  "lastVote":{"timestamp":1269486788526,"user":
   {"name":"Lamur","user_id":"68694999"}},"startPrice":0}]'
)

for(i in l) {
    print(i$votes)
}

我只是希望关闭数据votes

有什么想法吗?

这是我需要从中提取数据的更复杂的json:

json <- '[{
    "data": {
        "dailysummary": [{
            "date": {
                "pretty": "12:20 AM BST on June 28, 2017",
                "year": "2017",
                "mon": "06",
                "mday": "28",
                "hour": "00",
                "min": "20",
                "tzname": "Europe/London"
            },
            "fog": "0",
            "rain": "1",
            "snow": "0",
            "snowfallm": "",
            "snowfalli": "",
            "monthtodatesnowfallm": "",
            "monthtodatesnowfalli": "",
            "since1julsnowfallm": "",
            "since1julsnowfalli": "",
            "snowdepthm": "",
            "snowdepthi": "",
            "hail": "0",
            "thunder": "0",
            "tornado": "0",
            "meantempm": "16",
            "meantempi": "60",
            "meandewptm": "13",
            "meandewpti": "56",
            "meanpressurem": "997.19",
            "meanpressurei": "29.45",
            "meanwindspdm": "9",
            "meanwindspdi": "6",
            "meanwdire": "NW",
            "meanwdird": "326",
            "meanvism": "8.9",
            "meanvisi": "5.5",
            "humidity": "86",
            "maxtempm": "17",
            "maxtempi": "62",
            "mintempm": "14",
            "mintempi": "57",
            "maxhumidity": "100",
            "minhumidity": "77",
            "maxdewptm": "15",
            "maxdewpti": "59",
            "mindewptm": "10",
            "mindewpti": "50",
            "maxpressurem": "1001",
            "maxpressurei": "29.56",
            "minpressurem": "996",
            "minpressurei": "29.42",
            "maxwspdm": "18",
            "maxwspdi": "12",
            "minwspdm": "6",
            "minwspdi": "4",
            "maxvism": "10.0",
            "maxvisi": "6.2",
            "minvism": "4.5",
            "minvisi": "2.8",
            "gdegreedays": "10",
            "heatingdegreedays": "6",
            "coolingdegreedays": "0",
            "precipm": "0.0",
            "precipi": "0.00",
            "precipsource": "3Or6HourObs",
            "heatingdegreedaysnormal": "",
            "monthtodateheatingdegreedays": "",
            "monthtodateheatingdegreedaysnormal": "",
            "since1sepheatingdegreedays": "",
            "since1sepheatingdegreedaysnormal": "",
            "since1julheatingdegreedays": "",
            "since1julheatingdegreedaysnormal": "",
            "coolingdegreedaysnormal": "",
            "monthtodatecoolingdegreedays": "",
            "monthtodatecoolingdegreedaysnormal": "",
            "since1sepcoolingdegreedays": "",
            "since1sepcoolingdegreedaysnormal": "",
            "since1jancoolingdegreedays": "",
            "since1jancoolingdegreedaysnormal": ""
        }],
        "airport": "LCY"
    },
    "date": "2017-06-28"
},{
    "data": {
        "dailysummary": [{
            "date": {
                "pretty": "12:20 AM BST on June 29, 2017",
                "year": "2017",
                "mon": "06",
                "mday": "29",
                "hour": "00",
                "min": "20",
                "tzname": "Europe/London"
            },
            "fog": "0",
            "rain": "0",
            "snow": "0",
            "snowfallm": "",
            "snowfalli": "",
            "monthtodatesnowfallm": "",
            "monthtodatesnowfalli": "",
            "since1julsnowfallm": "",
            "since1julsnowfalli": "",
            "snowdepthm": "",
            "snowdepthi": "",
            "hail": "0",
            "thunder": "0",
            "tornado": "0",
            "meantempm": "16",
            "meantempi": "60",
            "meandewptm": "11",
            "meandewpti": "51",
            "meanpressurem": "995.85",
            "meanpressurei": "29.41",
            "meanwindspdm": "14",
            "meanwindspdi": "9",
            "meanwdire": "WSW",
            "meanwdird": "256",
            "meanvism": "10.0",
            "meanvisi": "6.2",
            "humidity": "71",
            "maxtempm": "18",
            "maxtempi": "64",
            "mintempm": "13",
            "mintempi": "55",
            "maxhumidity": "82",
            "minhumidity": "63",
            "maxdewptm": "11",
            "maxdewpti": "52",
            "mindewptm": "10",
            "mindewpti": "50",
            "maxpressurem": "999",
            "maxpressurei": "29.50",
            "minpressurem": "994",
            "minpressurei": "29.36",
            "maxwspdm": "24",
            "maxwspdi": "15",
            "minwspdm": "9",
            "minwspdi": "6",
            "maxvism": "10.0",
            "maxvisi": "6.2",
            "minvism": "10.0",
            "minvisi": "6.2",
            "gdegreedays": "10",
            "heatingdegreedays": "6",
            "coolingdegreedays": "0",
            "precipm": "0.0",
            "precipi": "0.00",
            "precipsource": "3Or6HourObs",
            "heatingdegreedaysnormal": "",
            "monthtodateheatingdegreedays": "",
            "monthtodateheatingdegreedaysnormal": "",
            "since1sepheatingdegreedays": "",
            "since1sepheatingdegreedaysnormal": "",
            "since1julheatingdegreedays": "",
            "since1julheatingdegreedaysnormal": "",
            "coolingdegreedaysnormal": "",
            "monthtodatecoolingdegreedays": "",
            "monthtodatecoolingdegreedaysnormal": "",
            "since1sepcoolingdegreedays": "",
            "since1sepcoolingdegreedaysnormal": "",
            "since1jancoolingdegreedays": "",
            "since1jancoolingdegreedaysnormal": ""
        }],
        "airport": "LCY"
    },
    "date": "2017-06-29"
}]'

l <- fromJSON(json, ,flatten=TRUE)

我需要将其转换为新的数据框:

airport key_date        ws      wd      tempi humidity
LCY     2017-06-28      x       x       x       x
LCY     2017-06-29      x       x       x       x

这是我的旧代码,但它不再适用于来自json的l数据框:

# Initialize a data frame.
weatherDf <- data.frame()

# Set key conditions for checking later.
keyConditions <- c("meanwindspdi", "meanwdird", "meantempm", "humidity")

for(i in l) {
    # Get the airport code.
    airport <- i$airport

    # Get the date.
    date <- as.POSIXct(as.numeric(as.character(i$date))/1000, origin="1970-01-01", tz="UTC-1")

    # Get the data in dailysummary only.
    dailySummary <- i$dailysummary

    # print(all(keyConditions %in% names(dailySummary)))

    # Make sure key conditions exist in dailySummary,
    # then use rbind or will get 'numbers of columns of arguments do not match'
    if (all(keyConditions %in% names(dailySummary))) {
        weatherDf <- rbind(weatherDf, ldply(
            list(dailySummary),
            function(x) c(airport, format(as.Date(date), "%Y-%m-%d"), x[["meanwindspdi"]], x[["meanwdird"]], x[["meantempm"]], x[["humidity"]])
        ))
    }
}

# Rename column names.
colnames(weatherDf) <- c("airport", "key_date", "ws", "wd", "tempi", 'humidity')

# Convert certain columns weatherDf type to numberic.
columns <-c("ws", "wd", "tempi", "humidity")
weatherDf[, columns] <- lapply(columns, function(x) as.numeric(weatherDf[[x]]))

修改

weatherDf <- data.frame()
for(i in 1:nrow(l)) {
    row <- l[i,]
    # do stuff with row

    # Get the date.
    date <- row$date

    # Get the airport code.
    airport <- row$data.airport
    print(airport)

    # Get the data in dailysummary only.
    dailySummary <- row$data.dailysummary

    # print(all(keyConditions %in% names(dailySummary)))
    weatherDf <- rbind(weatherDf, ldply(
        list(dailySummary),
        function(x) c(airport, format(as.Date(date), "%Y-%m-%d"), x[["meanwindspdi"]], x[["meanwdird"]], x[["meantempm"]], x[["humidity"]])
    ))
}

编辑2:

dailySummary <- row$data.dailysummary
print(names(dailySummary))

结果:

NULL

我想要得到的东西:

[1] "fog"                                "rain"                               "snow"                              
 [4] "snowfallm"                          "snowfalli"                          "monthtodatesnowfallm"              
 [7] "monthtodatesnowfalli"               "since1julsnowfallm"                 "since1julsnowfalli"                
[10] "snowdepthm"                         "snowdepthi"                         "hail"                              
[13] "thunder"                            "tornado"                            "meantempm"                         
[16] "meantempi"                          "meandewptm"                         "meandewpti"                        
[19] "meanpressurem"                      "meanpressurei"                      "meanwindspdm"                      
[22] "meanwindspdi"                       "meanwdire"                          "meanwdird"                         
[25] "meanvism"                           "meanvisi"                           "humidity"                          
[28] "maxtempm"                           "maxtempi"                           "mintempm"                          
[31] "mintempi"                           "maxhumidity"                        "minhumidity"                       
[34] "maxdewptm"                          "maxdewpti"                          "mindewptm"                         
[37] "mindewpti"                          "maxpressurem"                       "maxpressurei"                      
[40] "minpressurem"                       "minpressurei"                       "maxwspdm"                          
[43] "maxwspdi"                           "minwspdm"                           "minwspdi"                          
[46] "maxvism"                            "maxvisi"                            "minvism"                           
[49] "minvisi"                            "gdegreedays"                        "heatingdegreedays"                 
[52] "coolingdegreedays"                  "precipm"                            "precipi"                           
[55] "precipsource"                       "heatingdegreedaysnormal"            "monthtodateheatingdegreedays"      
[58] "monthtodateheatingdegreedaysnormal" "since1sepheatingdegreedays"         "since1sepheatingdegreedaysnormal"  
[61] "since1julheatingdegreedays"         "since1julheatingdegreedaysnormal"   "coolingdegreedaysnormal"           
[64] "monthtodatecoolingdegreedays"       "monthtodatecoolingdegreedaysnormal" "since1sepcoolingdegreedays"        
[67] "since1sepcoolingdegreedaysnormal"   "since1jancoolingdegreedays"         "since1jancoolingdegreedaysnormal"  
[70] "date.pretty"                        "date.year"                          "date.mon"                          
[73] "date.mday"                          "date.hour"                          "date.min"                          
[76] "date.tzname"                       
 [1] "fog"                                "rain"                               "snow"                              
 [4] "snowfallm"                          "snowfalli"                          "monthtodatesnowfallm"              
 [7] "monthtodatesnowfalli"               "since1julsnowfallm"                 "since1julsnowfalli"                
[10] "snowdepthm"                         "snowdepthi"                         "hail"                              
[13] "thunder"                            "tornado"                            "meantempm"                         
[16] "meantempi"                          "meandewptm"                         "meandewpti"                        
[19] "meanpressurem"                      "meanpressurei"                      "meanwindspdm"                      
[22] "meanwindspdi"                       "meanwdire"                          "meanwdird"                         
[25] "meanvism"                           "meanvisi"                           "humidity"                          
[28] "maxtempm"                           "maxtempi"                           "mintempm"                          
[31] "mintempi"                           "maxhumidity"                        "minhumidity"                       
[34] "maxdewptm"                          "maxdewpti"                          "mindewptm"                         
[37] "mindewpti"                          "maxpressurem"                       "maxpressurei"                      
[40] "minpressurem"                       "minpressurei"                       "maxwspdm"                          
[43] "maxwspdi"                           "minwspdm"                           "minwspdi"                          
[46] "maxvism"                            "maxvisi"                            "minvism"                           
[49] "minvisi"                            "gdegreedays"                        "heatingdegreedays"                 
[52] "coolingdegreedays"                  "precipm"                            "precipi"                           
[55] "precipsource"                       "heatingdegreedaysnormal"            "monthtodateheatingdegreedays"      
[58] "monthtodateheatingdegreedaysnormal" "since1sepheatingdegreedays"         "since1sepheatingdegreedaysnormal"  
[61] "since1julheatingdegreedays"         "since1julheatingdegreedaysnormal"   "coolingdegreedaysnormal"           
[64] "monthtodatecoolingdegreedays"       "monthtodatecoolingdegreedaysnormal" "since1sepcoolingdegreedays"        
[67] "since1sepcoolingdegreedaysnormal"   "since1jancoolingdegreedays"         "since1jancoolingdegreedaysnormal"  
[70] "date.pretty"                        "date.year"                          "date.mon"                          
[73] "date.mday"                          "date.hour"                          "date.min"                          
[76] "date.tzname"   

2 个答案:

答案 0 :(得分:1)

为什么不按l$votes访问数据?

您正在使用的for循环毫无意义。它没有将i定义为l,而是将数据框l的列提供给循环,即&#34;赢家&#34;,&#34;投票&# 34;,&#34; lastvote&#34;和&#34; startprice&#34;。

错误来自于您尝试使用$ - 使用字符向量l$winner建立索引,尝试打印l$winner$votes

编辑:这是您提供的新数据框的代码

day_n <- length(l$data.dailysummary) # number of days
weather <- NULL; # daily summary data will be saved here

for (i in 1:day_n){
  weather <- rbind(weather, c(ws=as.numeric(l$data.dailysummary[[i]]$meanwindspdi),
                              wd=as.numeric(l$data.dailysummary[[i]]$meanwdird),
                              tempi=as.numeric(l$data.dailysummary[[i]]$meantempm),
                              humidity=as.numeric(l$data.dailysummary[[i]]$humidity))
        )
}

# make matrix of weather data for every day
# l$data.dailysummary is a list, containing the weather data in data frames for each day
# these data frames are accessed by l$data.dailysummary[[i]], to get the desired data

out <- data.frame(airport=l$data.airport, key_date=as.Date(l$date), 
                  weather, stringsAsFactors = F)

我们会在问题中找到您要求的数据框:

> str(out)
'data.frame':   2 obs. of  6 variables:
 $ airport : chr  "LCY" "LCY"
 $ key_date: Date, format: "2017-06-28" "2017-06-29"
 $ ws      : num  6 9
 $ wd      : num  326 256
 $ tempi   : num  16 16
 $ humidity: num  86 71
> out
  airport   key_date ws  wd tempi humidity
1     LCY 2017-06-28  6 326    16       86
2     LCY 2017-06-29  9 256    16       71

答案 1 :(得分:0)

l $ votes是一个列表。如果你需要在具有这种格式的列表中循环遍历这些格式的多个,这可能使用相对较新的purrr包:

purrr::walk(l, ~ print(.x[[1]] ))

如果你想做一个循环,打印出你所展示的json结构中每个项目的整个数据框,这样的东西可以工作:

for(i in l){
  print(i[[1]])
}