John,

Again, it depends on how you want to use the data when making a design.

Specifically, for some purposes like graphing, the data is often best
formatted to make it possible.

I note your second column seems to be categorical and can easily be stored
as a factor which would make some graphs easy to group by. But as you have
two sets of data for each day, an alternate would be to have a different
approach, all on the same line.

Your third column is a tad hard to interpret. It looks like it contains
EITHER a list or other data structure containing four cutpoint values that
look numeric, OR it might contain four integers. As a guess, I would assume
you are viewing the data relative to some 2-D rectangle and you have
something like the X and Y coordinates of a lower corner and the similar X
and Y of an upper corner in one kind of row. As you note, you calculated
these cut points and want the other kind of row to report how many data
points violated these boundaries. 

If I am right (and perhaps I am not even in the ballpark)  then your numbers
suggest (on 1/2) that 100 were below 0.1 on X with 50 (perhaps overlapping)
being below 0.2 on Y and similarly 10 and 2 were above or the to right of
the upper pair of coordinates.

So how would you want this data stored? Using lists or vectors may work but
for a fixed number of exactly four, perhaps 4 columns would work as well.
But columns must contain only data of one kind so using numeric and int
together may require using just numeric. Alternately, you could have two
sets of 8 columns in every row and the four not being used can be NA. 

The right choice of data structures can match your use. Given your
experiences to date, sticking with simpler ones can make life easier. But,
as shown, you can use lists if careful and lists can contain any type.

Formatting output is a well-explored topic. One example is a
pprint::pprint() but others have similar functions such as in ramify.

And, if you did work in special environments like RSTUDIO (or the newer
POSIT) you could do your R coding from within a document with lots of
control over how it prints.


But, if you want to stick with basics, ...


-----Original Message-----
From: Sorkin, John <[email protected]> 
Sent: Thursday, June 4, 2026 8:26 PM
To: [email protected]; 'Rui Barradas' <[email protected]>; 'R-help'
<[email protected]>
Subject: Re: [R] Convert R object to a data frame

Avi,

The code generates data for each day of observation. For each day I want to
generate the number of subjects exposed to a value at, or above, a toxic
cutpoint. The table below shows an example of the information I want to
capture. The format of the output is of minor importance. What is important
is that I get the data from my code. Once I get the data I can work on
formatting it so it looks nice.

Date    Variable                                       Cutpoint
1/2/2020        Cutpoint Value            0.1   0.2       0.3    0.4
1/2/2020        Number  of Subjects     100       50    10        2
1/3/2020        Cutpoint Value            0.1     0.2    0.3     0.4
1/3/2020        Number  of Subjects       34      22     11      0
1/4/2020        Cutpoint Value            0.1    0.2     0.3     0.4
1/4/2020        Number  of Subjects      200     70     340      40

The table would look better if I could send this email in html format, but I
believe R help will reject formatted email.

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





________________________________________
From: [email protected] <[email protected]>
Sent: Thursday, June 4, 2026 6:30 PM
To: 'Rui Barradas'; Sorkin, John; 'R-help'
Subject: RE: [R] Convert R object to a data frame

It would help, John, if we understood what the resultant data.frame should
look like.
The structure looks like it contains nested lists including a list of 4
containing 4 lists of 2.

The contents range from int to num.

In a standard data.frame, you might want eight columns per row and would end
up with four rows.

You can now make data.frames CAREFULLY in which a column can contain
something complex such as a list or the output of a statistical function.
So, four columns each containing a list of 2 columns can be done.

Since you have a very specific data structure in mind, conversions could be
straightforward or not.

Here is a simpleminded attempt to replicate your first structure to generate
one row that unexpectedly does something a bit different:

myRow <-
  data.frame(
    alpha = list(criticalvalue=0.1, NumPeopleExposed=0),
    beta  = list(criticalvalue=0.2, NumPeopleExposed=0),
    gamma = list(criticalvalue=0.3, NumPeopleExposed=0),
    delta = list(criticalvalue=0.4, NumPeopleExposed=0)
  )

Note you could have initialized this from the other data structure by
putting in references to those values instead of my constants BUT the goal
was to see what it made and, well, it is not as expected:

> myRow
  alpha.criticalvalue alpha.NumPeopleExposed beta.criticalvalue
beta.NumPeopleExposed gamma.criticalvalue
1                 0.1                      0                0.2
0                 0.3
  gamma.NumPeopleExposed delta.criticalvalue delta.NumPeopleExposed
1                      0                 0.4                      0
> nrow(myRow)
[1] 1
> ncol(myRow)
[1] 8
> names(myRow)
[1] "alpha.criticalvalue"    "alpha.NumPeopleExposed" "beta.criticalvalue"
"beta.NumPeopleExposed"
[5] "gamma.criticalvalue"    "gamma.NumPeopleExposed" "delta.criticalvalue"
"delta.NumPeopleExposed"

You need to protect a sub-list from base R with something like the I()
function:

Is this version better?

myRow <-
  data.frame(
    alpha = I(list(criticalvalue=0.1, NumPeopleExposed=0)),
    beta  = I(list(criticalvalue=0.2, NumPeopleExposed=0)),
    gamma = I(list(criticalvalue=0.3, NumPeopleExposed=0)),
    delta = I(list(criticalvalue=0.4, NumPeopleExposed=0))
  )

> myRow
                 alpha beta gamma delta
criticalvalue      0.1  0.2   0.3   0.4
NumPeopleExposed     0    0     0     0
> nrow(myRow)
[1] 2
> ncol(myRow)
[1] 4
> names(myRow)
[1] "alpha" "beta"  "gamma" "delta"

Well, my one row now seems to be two rows with four per row.

> myRow$alpha
$criticalvalue
[1] 0.1

$NumPeopleExposed
[1] 0

> myRow$alpha$criticalvalue
[1] 0.1
> myRow$alpha$NumPeopleExposed
[1] 0

This seems to get you some of what you want. Something may be off as it
should not be two rows.

I would suggest you can possibly find a solution in base R but my experience
has been using various tidyverse functions as in dplyr to construct and use
these embedded things.

Do note the above are examples of making a single row and as mentioned, you
may want to combine multiple ones using various techniques such as rbind and
cbind.





-----Original Message-----
From: R-help <[email protected]> On Behalf Of Rui Barradas via
R-help
Sent: Thursday, June 4, 2026 5:19 PM
To: Sorkin, John <[email protected]>; R-help <[email protected]>
Subject: Re: [R] Convert R object to a data frame

À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
>
>
>
> ______________________________________________
> [email protected] 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

______________________________________________
[email protected] 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.

______________________________________________
[email protected] 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.

Reply via email to