[R] Convert R object to a data frame

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Jun 4 23:19:09 CEST 2026


Às 21:57 de 04/06/2026, Sorkin, John escreveu:
> I have an R object created lapply function within a by a by function
> 
>       results_by_date <- by(
>         data,
>          data[,"Date"],
>          function(subdata) {
>            lapply(
>            alertlevels,
>            check_cutpoints2,
>            data=subdata,
>            mycolumn=column
>            )
>          }
>       )
> 
> which has the following structure
> 
> $ 2020-08-14:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 0
>   $ 2020-08-15:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 1
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 0
>   $ 2020-08-16:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 15
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 6
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 6
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 6
>   $ 2020-08-17:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 58
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 20
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 7
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 2
>   $ 2020-08-18:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 79
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 45
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 16
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 6
>   $ 2020-08-19:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 187
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 100
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 61
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 38
>   $ 2020-08-20:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 121
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 31
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 13
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 4
>   $ 2020-08-21:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 17
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 8
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 5
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 3
>   $ 2020-08-22:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 35
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 12
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 6
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 2
>   $ 2020-08-23:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 32
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 15
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 8
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 4
>   $ 2020-08-24:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 15
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 5
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 1
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 0
>   $ 2020-08-25:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 43
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 26
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 13
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 5
>   $ 2020-08-26:List of 4
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.1
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.2
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.3
>    .. ..$ NumPeopleExposed: int 0
>    ..$ :List of 2
>    .. ..$ criticalvalue   : num 0.4
>    .. ..$ NumPeopleExposed: int 0
>   - attr(*, "dim")= int 13
>   - attr(*, "dimnames")=List of 1
>    ..$ data[, "Date"]: chr [1:13] "2020-08-14" "2020-08-15" "2020-08-16" "2020-08-17" ...
>   - attr(*, "call")= language by.data.frame(data = data, INDICES = data[, "Date"], FUN = function(subdata) {     lapply(alertlevels, check_cutp| __truncated__ ...
>   - attr(*, "class")= chr "by"
> NULL
> 
> I would like to convert the object to a dataframe. Can anyone suggest how I might accomplish this task?
> 
> Thank you,
> John
> 
> John David Sorkin M.D., Ph.D.
> Professor of Medicine, University of Maryland School of Medicine;
> Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center;
> Former PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center;
> Senior Statistician University of Maryland Center for Vascular Research;
> 
> Division of Gerontology, Geriatrics and Palliative Medicine,
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> Cell phone 443-418-5382
> 
> 
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

The problem is that you have a lapply() nested in by(), so you will have 
to rbind twice.
It seems that


results_by_date <- by(
   data,
   data[,"Date"],
   function(subdata) {
     lapply(
       alertlevels,
       check_cutpoints2,
       data=subdata,
       mycolumn=column
     ) |> do.call(rbind, args = _)
   }
) |> do.call(rbind, args = _)


can solve it.
Can you post the output of dput(results_by_date[1:2]) ?

Hope this helps,

Rui Barradas



More information about the R-help mailing list