简体   繁体   English

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

[英]R fromJSON - $ operator is invalid for atomic vectors

Why do I get this error below: 为什么在下面出现此错误:

Error: $ operator is invalid for atomic vectors

My dummy code for a simple test: 我的虚拟代码用于简单测试:

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)
}

I just want to get the data off votes 我只是想下车数据votes

Any ideas? 有任何想法吗?

This is my more complex json that I need to pull the data from: 这是我需要从中提取数据的更复杂的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)

I need to make it into a new dataframe: 我需要将其放入新的数据框:

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

This is my old code but it does not work with this l dataframe from json anymore: 这是我的旧代码,但是它不再适用于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]]))

EDIT: 编辑:

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"]])
    ))
}

Edit 2: 编辑2:

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

Result: 结果:

NULL

What I try to get: 我尝试得到的是:

[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"   

Why not just access the data by l$votes ? 为什么不仅仅通过l$votes访问数据?

The for loop you are using makes little sense. 您使用的for循环毫无意义。 It is not defining i as l , but instead feeds the columns of the data frame l into the loop, ie "winner", "votes", "lastvote" and "startprice". 它没有将i定义为l ,而是将数据帧l的列馈入循环,即“优胜者”,“投票”,“ lastvote”和“ startprice”。

The error comes from the fact that you try to use $ -indexing with the character vector l$winner , trying to print l$winner$votes . 错误来自以下事实:您尝试将$ -indexing与字符向量l$winner ,并尝试打印l$winner$votes

EDIT : This is the code for the new data frame you gave 编辑 :这是您给的新数据框的代码

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)

We get the data frame you asked for in the question: 我们得到您在问题中要求的数据框:

> 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

l$votes is a list. l $ votes是一个列表。 If you need to loop through multiples of these in a list having this format, this might work using the relatively new purrr package: 如果您需要在具有此格式的列表中遍历多个变量,则可以使用相对较新的purrr包进行工作:

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

If you want to do a loop, printing out the whole data frame for each item in the json structured like you showed, something like this could work: 如果要进行循环,请按照您显示的结构打印出json中每个项目的整个数据框,如下所示即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM