Re: [R] Convert two-dimensional array into a three-dimensional array.
OK. I'm not getting what you want, so feel free to ignore this if you think I've missed the point completely and don't want to waste your time. Won't be my first time clueless. A 3-D array can be thought of as as a "pile" of 2-D flats, so a 10 x 2 x 10 array consists of 10 2-d flats, each 10 x 2. So tell me what you want the first 10x2 flat to contain, then the second, etc. Here is a print representation of a 2 x 4 x 3 array that might help: > array(1:24, dim = c(2,4,3)) , , 1 [,1] [,2] [,3] [,4] [1,]1357 [2,]2468 , , 2 [,1] [,2] [,3] [,4] [1,]9 11 13 15 [2,] 10 12 14 16 , , 3 [,1] [,2] [,3] [,4] [1,] 17 19 21 23 [2,] 18 20 22 24 FWIW, it sounds to me like you just do something like: > dval <- matrix(1:8, nrow = 2) > dval [,1] [,2] [,3] [,4] [1,]1357 [2,]2468 > ar <- array(dval, dim = c(2,4,3)) > ar , , 1 [,1] [,2] [,3] [,4] [1,]1357 [2,]2468 , , 2 [,1] [,2] [,3] [,4] [1,]1357 [2,]2468 , , 3 [,1] [,2] [,3] [,4] [1,]1357 [2,]2468 since the 3rd array index itself provides the values you refer to. But this doesn't make sense to me, so I've probably misinterpreted. Cheers, Bert On Fri, Dec 8, 2023 at 2:58 PM Sorkin, John wrote: > Colleagues > > I want to convert a 10x2 array: > # create a 10x2 matrix. > datavals <- matrix(nrow=10,ncol=2) > datavals[,] <- rep(c(1,2),10)+c(rnorm(10),rnorm(10)) > datavals > > into a 10x3 array, ThreeDArray, dim(10,2,10). > > The values storede in ThreeDArray's first dimensions will be the data > stored in datavalues. > ThreeDArray[i,,] <- datavals[i,] > > The values storede in ThreeDArray's second dimensions will be the data > stored in datavalues. > ThreeDArray[,j,] <- datavals[,j] > > The data stored in ThreeDArray[,,1] will be 1, > The data stored in ThreeDArray[,,2] will be 2. > . . . > The data stored in ThreeDArray[,,10] will be 10. > > I have no idea how to code the coversion of the 10x2 matrix into a 10,2,10 > array. > I may be able to acomplish my mission by coding each line of the plan > described above, > but there has to be a more efficient and elegant way to accompish my goal. > > Many thanks for your help! > 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; > > 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 and Paliative Care, > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > > > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Convert two-dimensional array into a three-dimensional array.
Colleagues I want to convert a 10x2 array: # create a 10x2 matrix. datavals <- matrix(nrow=10,ncol=2) datavals[,] <- rep(c(1,2),10)+c(rnorm(10),rnorm(10)) datavals into a 10x3 array, ThreeDArray, dim(10,2,10). The values storede in ThreeDArray's first dimensions will be the data stored in datavalues. ThreeDArray[i,,] <- datavals[i,] The values storede in ThreeDArray's second dimensions will be the data stored in datavalues. ThreeDArray[,j,] <- datavals[,j] The data stored in ThreeDArray[,,1] will be 1, The data stored in ThreeDArray[,,2] will be 2. . . . The data stored in ThreeDArray[,,10] will be 10. I have no idea how to code the coversion of the 10x2 matrix into a 10,2,10 array. I may be able to acomplish my mission by coding each line of the plan described above, but there has to be a more efficient and elegant way to accompish my goal. Many thanks for your help! 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; 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 and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Convert character date time to R date-time variable.
On 08/12/2023 4:30 p.m., David Winsemius wrote: On 12/7/23 08:21, Sorkin, John wrote: Colleagues, I have a matrix of character data that represents date and time. The format of each element of the matrix is "2020-09-17_00:00:00" How can I convert the elements into a valid R date-time constant? You will not be able to store these datetime values in an R matrix, at least as class POSIXct. You could with class POSIXlt, but I've not seen it used before but it does appear possible since matrices can contain lists. R matrices do no provide the capability to assign attributes, so only atomic types and lists can be elements. If you wanted to maintain the same structure, your first step might be to coerce to a data.frame and then proceed, or to first construct a vector and then use as the first argument to `matrix`. The general sentiment is correct (it's hard to put POSIXct elements in matrices), but it's not impossible. Almost all R objects can have attributes. For example, m <- matrix(Sys.time(), 2,2) converts the time to a numerical value, but you can view it as a POSIXlt object using class(m) <- c("POSIXct", class(m)) Subsetting works, e.g. m[1,1] is a time, etc. Duncan Murdoch Best; David 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; 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 and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Convert character date time to R date-time variable.
On 12/7/23 08:21, Sorkin, John wrote: Colleagues, I have a matrix of character data that represents date and time. The format of each element of the matrix is "2020-09-17_00:00:00" How can I convert the elements into a valid R date-time constant? You will not be able to store these datetime values in an R matrix, at least as class POSIXct. You could with class POSIXlt, but I've not seen it used before but it does appear possible since matrices can contain lists. R matrices do no provide the capability to assign attributes, so only atomic types and lists can be elements. If you wanted to maintain the same structure, your first step might be to coerce to a data.frame and then proceed, or to first construct a vector and then use as the first argument to `matrix`. Best; David 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; 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 and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Convert character date time to R date-time variable.
> Ebert,Timothy Aaron > on Thu, 7 Dec 2023 16:29:09 + writes: > Look at the lubridate package in R. Regards, Tim Absolutely *un*needed here !! - as others mention in this thread. Very simple with base R: > strptime("2020-09-17_00:00:00", format = "%Y-%m-%d_%H:%M:%S") [1] "2020-09-17 CEST" > (in my time zone). > -Original Message- From: R-help > On Behalf Of Sorkin, John > Sent: Thursday, December 7, 2023 11:22 AM To: > r-help@r-project.org (r-help@r-project.org) > Subject: [R] Convert character date > time to R date-time variable. > [External Email] > Colleagues, > I have a matrix of character data that represents date and > time. The format of each element of the matrix is > "2020-09-17_00:00:00" How can I convert the elements into > a valid R date-time constant? > 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; > 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 and Paliative Care, 10 North > Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and > more, see https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.r-project.org/posting-guide.html and provide > commented, minimal, self-contained, reproducible code. > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and > more, see https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide > commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.