
# this was from a stack overflow question that was deleted. 



DF <- read.table(header=TRUE, stringsAsFactors=FALSE, text=
"firstname  lastname    latitude    longitude   exp cost    gender
 Ace        Threadgill    34.68259    -82.839539  1   1   F
 Abe         Millett    36.75502    -76.059196  1   1   F
 Abe         Millett    43.637611   -70.251732  1   1   F
 Andy         Pettitt    36.75502    -76.059196  1   1   F
 Aaron        Jacobs    32.778149   -96.795402  1   1   M")


Your data needs to be in a list structure analalogous to how you would like your JSON object. 
Heres an example 


    library(data.table)
    library(rjson)
    DT <- data.table(DF)


    DT[, name := list(list(c(firstname=firstname, lastname=lastname))), by=list(firstname, lastname)]
    DT[, geo  := list(list(c(latitude=latitude, longitude=longitude))), by=list(latitude, longitude)]

    # you can then delete the un-needed columns, or create a new DT, or filter them in your JSON call. Up to you. 
    DT[, c("firstname", "lastname", "latitude", "longitude") := NULL]

    # you can also re-order if you'd like 
    setcolorder(DT, c("name", "geo", "exp", "cost", "gender"))

    DT.as.a.list <- apply(DT, 1, as.list)
    output.json <- toJSON(DT.as.a.list)

    cat(output.json)

    [{"name":{"firstname":"Ace","lastname":"Threadgill"},"geo":{"latitude":34.68259,"longitude":-82.839539},"exp":1,"cost":1,"gender":"F"},{"name":{"firstname":"Abe","lastname":"Millett"},"geo":{"latitude":36.75502,"longitude":-76.059196},"exp":1,"cost":1,"gender":"F"},{"name":{"firstname":"Abe","lastname":"Millett"},"geo":{"latitude":43.637611,"longitude":-70.251732},"exp":1,"cost":1,"gender":"F"},{"name":{"firstname":"Andy","lastname":"Pettitt"},"geo":{"latitude":36.75502,"longitude":-76.059196},"exp":1,"cost":1,"gender":"F"},{"name":{"firstname":"Aaron","lastname":"Jacobs"},"geo":{"latitude":32.778149,"longitude":-96.795402},"exp":1,"cost":1,"gender":"M"}]


----------------------


## REQUIREMENTS: 

 I would like to combine firstname and lastname into a object name and combine lat and long into geo.


      [{name:{firstname:"A",
                lastname:"Threadgill"},
          geo:{
                latitude: "34.68259",
                longitude: "-82.839539"}
          exp:1,
          cost:1,
          gender:"F"},
         {name:{firstname:"A",
                lastname:"Millett"},
          geo:{latitude: "43.637611",
               longitude: "-70.251732"},
          exp:1,
          cost:1,
          gender:"F"},
          ...
          ]
