arguments imply differing number of rows in Data Frame Error

Hello ,

I am getting error while creating data frame with multiple rows.

request_body <- data.frame(DivisionId=c(DivisionId),PrimoId=c(PrimoId),Ename=c(Ename),Sname=c(Sname),StartDepth=c(StartDepth),EndDepth=c(EndDepth),SidetrackNum=c(SidetrackNum))
> bodyTest <- toJSON(request_body)
> bodyTest
[{"DivisionId":10,"PrimoId":73413,"Ename":"IRK_DRILLING","Sname":"RATE_OF_PENETRATION","StartDepth":0,"EndDepth":3000,"SidetrackNum":-1},{"DivisionId":63,"PrimoId":88245,"Ename":"IRK_DRILLING","Sname":"HOOK_LOAD","StartDepth":0,"EndDepth":6000,"SidetrackNum":-1}] 
> r <- POST(url, body = bodyTest, encode = "json")
> resultRaw <- content(r, as = "text")
> dt <- fromJSON(resultRaw)
> testB2 <-
+     data.frame(
+         DivisionId=unlist(dt$DivisionId),
+         PrimoId=unlist(dt$PrimoId),
+         Wits=unlist(dt$Wits),
+         Adname=unlist(dt$Adname),
+         Mds = unlist(dt$Array$Mds), 
+         Tms = unlist(dt$Array$Tms),
+         Vls = unlist(dt$Array$Vls), 
+         Ads = unlist(dt$Array$Ads))

Error in data.frame(DivisionId = unlist(dt$DivisionId), PrimoId = unlist(dt$PrimoId),  : 
  arguments imply differing number of rows: 2, 3625

Thanks,
Navjeet

The error occurs when you create this data frame,

     data.frame(
         DivisionId=unlist(dt$DivisionId),
         PrimoId=unlist(dt$PrimoId),
         Wits=unlist(dt$Wits),
         Adname=unlist(dt$Adname),
         Mds = unlist(dt$Array$Mds), 
         Tms = unlist(dt$Array$Tms),
         Vls = unlist(dt$Array$Vls), 
         Ads = unlist(dt$Array$Ads)
     )

Data frame columns/variables must all have the same length. The error message "arguments imply differing number of rows" is telling you that the lengths of the variables you're setting up vary in length (and at least one column is 2 in length, and another 3625).

I'd look more closely that what's in your unlist(...) calls to figure out what's going on.


I personally like to work with json files in R with purrr and jsonlite. Jenny wrote a nice tutorial on this here;

https://jennybc.github.io/purrr-tutorial/ex26_ny-food-market-json.html

And it would be helpful if you could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

Yes it provide result for two different values and in JSON i see as well two different array.

Basically i want to create data frame for this.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.