Re: Convert R object to a data frame
<[email protected]> Thu, 4 Jun 2026 18:30:49 -0400
| Newsgroups | gmane.comp.lang.r.general |
|---|---|
| Message-ID | <[email protected]> |
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 con=
taining 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 en=
d up with four rows. =
You can now make data.frames CAREFULLY in which a column can contain someth=
ing complex such as a list or the output of a statistical function. So, fou=
r 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 generat=
e one row that unexpectedly does something a bit different:
myRow <-
data.frame(
alpha =3D list(criticalvalue=3D0.1, NumPeopleExposed=3D0),
beta =3D list(criticalvalue=3D0.2, NumPeopleExposed=3D0),
gamma =3D list(criticalvalue=3D0.3, NumPeopleExposed=3D0),
delta =3D list(criticalvalue=3D0.4, NumPeopleExposed=3D0)
)
Note you could have initialized this from the other data structure by putti=
ng in references to those values instead of my constants BUT the goal was t=
o see what it made and, well, it is not as expected:
> myRow
alpha.criticalvalue alpha.NumPeopleExposed beta.criticalvalue beta.NumPeo=
pleExposed 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() func=
tion:
Is this version better?
myRow <-
data.frame(
alpha =3D I(list(criticalvalue=3D0.1, NumPeopleExposed=3D0)),
beta =3D I(list(criticalvalue=3D0.2, NumPeopleExposed=3D0)),
gamma =3D I(list(criticalvalue=3D0.3, NumPeopleExposed=3D0)),
delta =3D I(list(criticalvalue=3D0.4, NumPeopleExposed=3D0))
)
> 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 sho=
uld not be two rows.
I would suggest you can possibly find a solution in base R but my experienc=
e has been using various tidyverse functions as in dplyr to construct and u=
se 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 a=
nd 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
=C3=80s 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=3Dsubdata,
> mycolumn=3Dcolumn
> )
> }
> )
> =
> 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")=3D int 13
> - attr(*, "dimnames")=3DList of 1
> ..$ data[, "Date"]: chr [1:13] "2020-08-14" "2020-08-15" "2020-08-16" =
"2020-08-17" ...
> - attr(*, "call")=3D language by.data.frame(data =3D data, INDICES =3D =
data[, "Date"], FUN =3D function(subdata) { lapply(alertlevels, check_c=
utp| __truncated__ ...
> - attr(*, "class")=3D 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 Medica=
l Center Geriatrics Research, Education, and Clinical Center;
> Former PI Biostatistics and Informatics Core, University of Maryland Scho=
ol 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=3Dsubdata,
mycolumn=3Dcolumn
) |> do.call(rbind, args =3D _)
}
) |> do.call(rbind, args =3D _)
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.ht=
ml
and provide commented, minimal, self-contained, reproducible code.