Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread Duncan Murdoch

On 09/07/2021 3:37 p.m., Laurent Rhelp wrote:


Very effective solution, I hope I remember that for the nex time.


The key things to remember are that in R a "list" object is a vector 
whose elements are R objects, and that matrices are just vectors with 
dimension attributes.


Those two ideas are each useful for other things, too!

Duncan Murdoch


Thank you


Le 09/07/2021 à 19:50, David Winsemius a écrit :


On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

   I have a list init_l containing 16 dataframes and I want to create
a matrix 4 x 4 from this list with a dataframe in every cell of the
matrix. I succeeded to do that but my loop is very uggly (cf. below).
Could somebody help me to write nice R code to do this loop ?

Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
   data.frame( V1 = rnorm(3),
   V2 = rnorm(3),
   V3 = rnorm(3)
     )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two
dimensional structure



dim(init_l) <- c(n,n)
init_l[ 2,2]

[[1]]
   V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344


is.matrix(init_l)

[1] TRUE






__
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] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp



Very effective solution, I hope I remember that for the nex time.
Thank you


Le 09/07/2021 à 19:50, David Winsemius a écrit :


On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create 
a matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two 
dimensional structure



> dim(init_l) <- c(n,n)
> init_l[ 2,2]
[[1]]
  V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344

> is.matrix(init_l)
[1] TRUE




--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

__
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] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp


You are right, this extra level disturbed me.
Very impressive this solution, thank you very much.


Le 09/07/2021 à 19:50, Bill Dunlap a écrit :
> Try
>    matrix(init_l, nrow=4, ncol=4, 
> dimnames=list(c("X1","X2","X3","X4"),c("X1","X2","X3","X4")))
> It doesn't give exactly what your code does, but your code introduces 
> an extra level of "list", which you may not want.
>
> -Bill
>
> On Fri, Jul 9, 2021 at 10:40 AM Laurent Rhelp  > wrote:
>
> Dear R-Help-list,
>
>    I have a list init_l containing 16 dataframes and I want to
> create a
> matrix 4 x 4 from this list with a dataframe in every cell of the
> matrix. I succeeded to do that but my loop is very uggly (cf. below).
> Could somebody help me to write nice R code to do this loop ?
>
> Thank you very much
>
> Laurent
>
>
> ##
> ## mock data, 16 dataframes in a list
> ##
> init_l <- lapply( seq(1,16) , function(x) {
>    data.frame( V1 = rnorm(3),
>    V2 = rnorm(3),
>    V3 = rnorm(3)
>  )
> })
>
> ##
> ## lists matrix creation with n = 4 columns and n = 4 rows
> ##
> n <- 4
> ## an example of row to create the matrix with lists in the cells
> one_row <- rbind( rep( list(rep(list(1),3)) , n) )
> mymat <- do.call( "rbind" , rep( list(one_row) , n) )
>
> ##
> ## The UGGLY loop I would like to improve:
> ##
>
> ## populate the matrix
> k <- 1
> for( i in 1:n){
>    for( j in 1:n){
>  mymat[i,j][[1]] <- list( init_l[[ k ]] )
>  k <- k+1
>    }
> }
>
> colnames(mymat) <- c("X1", "X2", "X3", "X3")
> rownames(mymat) <- c("X1", "X2", "X3", "X4")
>
>
> mymat
>
> # X1 X2 X3 X3
> # X1 List,1 List,1 List,1 List,1
> # X2 List,1 List,1 List,1 List,1
> # X3 List,1 List,1 List,1 List,1
> # X4 List,1 List,1 List,1 List,1
>
>
> #
> # verification, it works
> #
> mymat[2,2]
> init_l[[6]]
>
> ##
> init_l[[6]]
>
> library(tidyverse)
> mymat.t <- as.tibble(mymat)
> mymat.t
> unnest(mymat.t[2,2],cols="X2")[[1]][[1]]
>
> mymat.df <- as.data.frame(mymat)
> mymat.df[2,2][[1]][[1]]
>
>
> thx
>
>
>
> -- 
> L'absence de virus dans ce courrier électronique a été vérifiée
> par le logiciel antivirus Avast.
> https://www.avast.com/antivirus 
>
> __
> 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.
>



-- 
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

[[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.


Re: [R] How to create a matrix from a list without a for loop

2021-07-09 Thread David Winsemius



On 7/9/21 10:40 AM, Laurent Rhelp wrote:

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create a 
matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4




Just assign a dimension attribute and you will have your two dimensional 
structure



> dim(init_l) <- c(n,n)
> init_l[ 2,2]
[[1]]
  V1 V2 V3
1 -1.4103259  1.9214184 -0.1590919
2  0.1899490  0.3842191  2.4502078
3 -0.4282764 -0.9992190  1.5384344

> is.matrix(init_l)
[1] TRUE

--

David


## an example of row to create the matrix with lists in the cells
one_row <- rbind( rep( list(rep(list(1),3)) , n) )
mymat <- do.call( "rbind" , rep( list(one_row) , n) )

##
## The UGGLY loop I would like to improve:
##

## populate the matrix
k <- 1
for( i in 1:n){
  for( j in 1:n){
    mymat[i,j][[1]] <- list( init_l[[ k ]] )
    k <- k+1
  }
}

colnames(mymat) <- c("X1", "X2", "X3", "X3")
rownames(mymat) <- c("X1", "X2", "X3", "X4")


mymat

# X1 X2 X3 X3
# X1 List,1 List,1 List,1 List,1
# X2 List,1 List,1 List,1 List,1
# X3 List,1 List,1 List,1 List,1
# X4 List,1 List,1 List,1 List,1


#
# verification, it works
#
mymat[2,2]
init_l[[6]]

##
init_l[[6]]

library(tidyverse)
mymat.t <- as.tibble(mymat)
mymat.t
unnest(mymat.t[2,2],cols="X2")[[1]][[1]]

mymat.df <- as.data.frame(mymat)
mymat.df[2,2][[1]][[1]]


thx





__
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] How to create a matrix from a list without a for loop

2021-07-09 Thread Bill Dunlap
Try
   matrix(init_l, nrow=4, ncol=4,
dimnames=list(c("X1","X2","X3","X4"),c("X1","X2","X3","X4")))
It doesn't give exactly what your code does, but your code introduces an
extra level of "list", which you may not want.

-Bill

On Fri, Jul 9, 2021 at 10:40 AM Laurent Rhelp  wrote:

> Dear R-Help-list,
>
>I have a list init_l containing 16 dataframes and I want to create a
> matrix 4 x 4 from this list with a dataframe in every cell of the
> matrix. I succeeded to do that but my loop is very uggly (cf. below).
> Could somebody help me to write nice R code to do this loop ?
>
> Thank you very much
>
> Laurent
>
>
> ##
> ## mock data, 16 dataframes in a list
> ##
> init_l <- lapply( seq(1,16) , function(x) {
>data.frame( V1 = rnorm(3),
>V2 = rnorm(3),
>V3 = rnorm(3)
>  )
> })
>
> ##
> ## lists matrix creation with n = 4 columns and n = 4 rows
> ##
> n <- 4
> ## an example of row to create the matrix with lists in the cells
> one_row <- rbind( rep( list(rep(list(1),3)) , n) )
> mymat <- do.call( "rbind" , rep( list(one_row) , n) )
>
> ##
> ## The UGGLY loop I would like to improve:
> ##
>
> ## populate the matrix
> k <- 1
> for( i in 1:n){
>for( j in 1:n){
>  mymat[i,j][[1]] <- list( init_l[[ k ]] )
>  k <- k+1
>}
> }
>
> colnames(mymat) <- c("X1", "X2", "X3", "X3")
> rownames(mymat) <- c("X1", "X2", "X3", "X4")
>
>
> mymat
>
> # X1 X2 X3 X3
> # X1 List,1 List,1 List,1 List,1
> # X2 List,1 List,1 List,1 List,1
> # X3 List,1 List,1 List,1 List,1
> # X4 List,1 List,1 List,1 List,1
>
>
> #
> # verification, it works
> #
> mymat[2,2]
> init_l[[6]]
>
> ##
> init_l[[6]]
>
> library(tidyverse)
> mymat.t <- as.tibble(mymat)
> mymat.t
> unnest(mymat.t[2,2],cols="X2")[[1]][[1]]
>
> mymat.df <- as.data.frame(mymat)
> mymat.df[2,2][[1]][[1]]
>
>
> thx
>
>
>
> --
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> https://www.avast.com/antivirus
>
> __
> 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] How to create a matrix from a list without a for loop

2021-07-09 Thread Laurent Rhelp

Dear R-Help-list,

  I have a list init_l containing 16 dataframes and I want to create a 
matrix 4 x 4 from this list with a dataframe in every cell of the 
matrix. I succeeded to do that but my loop is very uggly (cf. below). 
Could somebody help me to write nice R code to do this loop ?


Thank you very much

Laurent


##
## mock data, 16 dataframes in a list
##
init_l <- lapply( seq(1,16) , function(x) {
  data.frame( V1 = rnorm(3),
  V2 = rnorm(3),
  V3 = rnorm(3)
    )
})

##
## lists matrix creation with n = 4 columns and n = 4 rows
##
n <- 4
## an example of row to create the matrix with lists in the cells
one_row <- rbind( rep( list(rep(list(1),3)) , n) )
mymat <- do.call( "rbind" , rep( list(one_row) , n) )

##
## The UGGLY loop I would like to improve:
##

## populate the matrix
k <- 1
for( i in 1:n){
  for( j in 1:n){
    mymat[i,j][[1]] <- list( init_l[[ k ]] )
    k <- k+1
  }
}

colnames(mymat) <- c("X1", "X2", "X3", "X3")
rownames(mymat) <- c("X1", "X2", "X3", "X4")


mymat

# X1 X2 X3 X3
# X1 List,1 List,1 List,1 List,1
# X2 List,1 List,1 List,1 List,1
# X3 List,1 List,1 List,1 List,1
# X4 List,1 List,1 List,1 List,1


#
# verification, it works
#
mymat[2,2]
init_l[[6]]

##
init_l[[6]]

library(tidyverse)
mymat.t <- as.tibble(mymat)
mymat.t
unnest(mymat.t[2,2],cols="X2")[[1]][[1]]

mymat.df <- as.data.frame(mymat)
mymat.df[2,2][[1]][[1]]


thx



--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
https://www.avast.com/antivirus

__
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.