Re: [R] data.frame with a column containing an array

2023-05-29 Thread Georg Kindermann
Maybe a better solution instead of "don't do it!" might be to change:

x[[j]] <- if(length(dim(xj)) != 2L) xj[i] else xj[i, , drop = FALSE]

at: 
https://github.com/wch/r-source/blob/trunk/src/library/base/R/dataframe.R#L712

to:

x[[j]] <- if(length(dim(xj)) < 2L) xj[i] else do.call("[",c(list(xj,i), 
rep(list(bquote()), length(dim(xj))-1L), list(drop = FALSE)))

Would this be possible and work?
Thanks!
Georg

 

Gesendet: Freitag, 26. Mai 2023 um 21:05 Uhr
Von: "Bert Gunter" 
An: "Georg Kindermann" 
Cc: "Rui Barradas" , r-help@r-project.org, "Jeff 
Newmiller" 
Betreff: Re: Re: Re: [R] data.frame with a column containing an array
Please refer to Jeff Newmiller's response. I believe that your further
exploration of indexing a data frame containing an array column
demonstrates his point: don't do it!

As this discussion could now devolve into a morass of personal opinion
(such as the above), if you or others care to continue, please do so
**offlist**. However, I have nothing further to say in any case.

Cheers,
Bert

__
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] data.frame with a column containing an array

2023-05-27 Thread Georg Kindermann
Dear Bert!

Thanks for the answer.
Just let me summarize the current (4.3.0) behavior.


DF <- data.frame(id = 1:2)
DF[["ar"]] <- array(1:8, c(2,2,2))

#Converts array to vector
#Removes dim and takes selected elements from first column
DF[1,]
#  id ar
#1  1  1

str(DF[1,])
#'data.frame':   1 obs. of  2 variables:
# $ id: int 1
# $ ar: int 1


#Converts array to vector
#Removes dim and takes selected elements
DF[c(TRUE, FALSE),]
#  id ar
#1  1  1
#Warning message:
#In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x,  :
#  corrupt data frame: columns will be truncated or padded with NAs

str(DF[c(TRUE, FALSE),])
#'data.frame':   1 obs. of  2 variables:
# $ id: int 1
# $ ar: int  1 3 5 7


#Converts array to vector
#Removes dim and takes selected elements from first column and
#adds all elements form the remaining data
DF[-2,]
#  id ar
#1  1  1
#Warning message:
#In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x,  :
#  corrupt data frame: columns will be truncated or padded with NAs

str(DF[-2,])
#'data.frame':   1 obs. of  2 variables:
# $ id: int 1
# $ ar: int  1 3 4 5 6 7 8


#Converts array to vector
#Removes dim but keeps all elements
DF[TRUE,]
#  id ar
#1  1  1
#2  2  2
#Warning message:
#In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x,  :
#  corrupt data frame: columns will be truncated or padded with NAs

str(DF[TRUE,])
#'data.frame':   2 obs. of  2 variables:
# $ id: int  1 2
# $ ar: int  1 2 3 4 5 6 7 8


Kind regards,
Georg


Gesendet: Donnerstag, 25. Mai 2023 um 19:15 Uhr
Von: "Bert Gunter" 
An: "Georg Kindermann" 
Cc: "Rui Barradas" , r-help@r-project.org
Betreff: Re: Re: [R] data.frame with a column containing an array

I really don't know. I would call it a request for extended capabilities of 
[.data.frame, rather than a feature or bug. But maybe wiser heads than mine who 
monitor this list can sort it out.
 
-- Bert 

__
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] data.frame with a column containing an array

2023-05-25 Thread Georg Kindermann
So is this an expected behavior or is it a bug which should be reported 
somewhere else?

Thanks!
Georg 
 
 

Gesendet: Dienstag, 09. Mai 2023 um 19:28 Uhr
Von: "Bert Gunter" 
An: "Georg Kindermann" 
Cc: "Rui Barradas" , r-help@r-project.org
Betreff: Re: [R] data.frame with a column containing an array

 
 
I think the following may provide a clearer explanation:
 
subs <- c(1,3)
DFA <- data.frame(id = 1:3)
ar <- array(1:12, c(3,2,2))
## yielding
> ar
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12
 
## array subscripting gives
> ar[subs,,]
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    9   12
 
## Now with df's
> DFA[["ar"]] <- ar
>
> DFM <- data.frame(id = 1:3)
> DFM[["M"]] <- matrix(1:6, nc =2)
>
> str(DFM)
'data.frame': 3 obs. of  2 variables:
 $ id: int  1 2 3
 $ M : int [1:3, 1:2] 1 2 3 4 5 6
> str(DFA)
'data.frame': 3 obs. of  2 variables:
 $ id: int  1 2 3
 $ ar: int [1:3, 1:2, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
>
> ## But the data frame print method for these give
> DFM  
  id M.1 M.2
1  1   1   4
2  2   2   5
3  3   3   6
> DFA
  id ar.1 ar.2 ar.3 ar.4
1  1    1    4    7   10
2  2    2    5    8   11
3  3    3    6    9   12
>
> ## [.data.frame subscripting gives
> DFA[subs,]
  id ar
1  1  1
3  3  3
> DFM[subs,]
  id M.1 M.2
1  1   1   4
3  3   3   6
>
> ## but  explicit array subscripting of course works
> DFA$ar[match(subs,DFA$id),,]
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    9   12
 
 
Cheers,
Bert 

__
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] data.frame with a column containing an array

2023-05-09 Thread Georg Kindermann
Thanks!
With data.table I'm not able to create it.

DT <- data.table::data.table(id = 1:2)
DT$ar <- array(1:8, c(2,2,2))
#Error in set(x, j = name, value = value) : 
#  Supplied 8 items to be assigned to 2 items of column 'ar'. If you wish to 
'recycle' the RHS please use rep() to make this intent clear to readers of your 
code.

DFA <- data.frame(id = 1:2)
DFA[["ar"]] <- array(1:8, c(2,2,2))
data.table::as.data.table(DFA)
#Error in setDT(ans, key = key) : 
#  All elements in argument 'x' to 'setDT' must be of same length, but the 
profile of input lengths (length:frequency) is: [0:1, 2:2]
#The first entry with fewer than 2 entries is 3

But with tibble it works as expected.

TI <- tibble::tibble(id = 1:2, ar = array(1:8, c(2,2,2)))
str(TI[1,])
#tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
# $ id: int 1
# $ ar: int [1, 1:2, 1:2] 1 3 5 7

But it would be nice if something similar would also work with data.frame.

Georg
 
 
 

Gesendet: Dienstag, 09. Mai 2023 um 16:56 Uhr
Von: "Bert Gunter" 
An: "Georg Kindermann" 
Cc: "Rui Barradas" , r-help@r-project.org
Betreff: Re: [R] data.frame with a column containing an array

Right ... that's what I thought you meant.
 
I'm pretty sure -- but not certain -- that columns of matrices are treated 
specially by [.data.frame, so that you have to explicitly index a higher 
dimensional array, e.g. like this:
 
subs <- c(1,3)
DFA <- data.frame(id = 1:3)
DFA[["ar"]] <- array(1:12, c(3,2,2))

DFA$ar[match(subs,DFA$id),,]
##yielding:
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    9   12
 
You might check, e.g. the "data.table" package, to see if it indexes as you 
would like with columns that contain arrays.
 
Alternatively, and perhaps preferably depending on your use case, you may wish 
to create a wholly different data structure or just treat the data frame as a 
list from the start. Data frames/matrix-like data structures are convenient and 
appropriate a lot of the time, but not always. R, like any flexible programming 
language, allows you -- even encourages you -- to create other data structures 
that fit your needs.
 
Cheers,
Bert 

__
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] data.frame with a column containing an array

2023-05-09 Thread Georg Kindermann
Thanks!
 
No, to be consistent with what I get with a matrix I think it should be like:

x <- data.frame(id = DFA$id[1])
x$ar <- DFA$ar[1, , , drop = FALSE]

str(x)
#'data.frame': 1 obs. of 2 variables:
# $ id: int 1
# $ ar: int [1, 1:2, 1:2] 1 3 5 7

Georg
 
 

Gesendet: Dienstag, 09. Mai 2023 um 09:32 Uhr
Von: "Rui Barradas" 
An: "Georg Kindermann" , r-help@r-project.org
Betreff: Re: [R] data.frame with a column containing an array
Às 11:52 de 08/05/2023, Georg Kindermann escreveu:
> Dear list members,
>
> when I create a data.frame containing an array I had expected, that I get a 
> similar result, when subsetting it, like having a matrix in a data.frame. But 
> instead I get only the first element and not all values of the remaining 
> dimensions. Differences are already when creating the data.frame, where I can 
> use `I` in case of a matrix but for an array I am only able to insert it in a 
> second step.
>
> DFA <- data.frame(id = 1:2)
> DFA[["ar"]] <- array(1:8, c(2,2,2))
>
> DFA[1,]
> # id ar
> #1 1 1
>
> DFM <- data.frame(id = 1:2, M = I(matrix(1:4, 2)))
>
> DFM[1,]
> # id M.1 M.2
> #1 1 1 3
>
> The same when trying to use merge, where only the first value is kept.
>
> merge(DFA, data.frame(id = 1))
> # id ar
> #1 1 1
>
> merge(DFM, data.frame(id = 1))
> # id M.1 M.2
> #1 1 1 3
>
> Is there a way to use an array in a data.frame like I can use a matrix in a 
> data.frame?
>
> I am using R version 4.3.0.
>
> Kind regards,
> Georg
>
> __
> 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[http://www.R-project.org/posting-guide.html]
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Are you looking for something like this?


DFA <- data.frame(id = 1:2)
DFA[["ar"]] <- array(1:8, c(2,2,2))

DFA$ar[1, , ]
#> [,1] [,2]
#> [1,] 1 5
#> [2,] 3 7


Hope this helps,

Rui Barradas
 

__
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] data.frame with a column containing an array

2023-05-09 Thread Georg Kindermann
Dear list members,

when I create a data.frame containing an array I had expected, that I get a 
similar result, when subsetting it, like having a matrix in a data.frame. But 
instead I get only the first element and not all values of the remaining 
dimensions. Differences are already when creating the data.frame, where I can 
use `I` in case of a matrix but for an array I am only able to insert it in a 
second step.

DFA <- data.frame(id = 1:2)
DFA[["ar"]] <- array(1:8, c(2,2,2))

DFA[1,]
#  id ar
#1  1  1

DFM <- data.frame(id = 1:2, M = I(matrix(1:4, 2)))

DFM[1,]
#  id M.1 M.2
#1  1   1   3

The same when trying to use merge, where only the first value is kept.

merge(DFA, data.frame(id = 1))
#  id ar
#1  1  1

merge(DFM, data.frame(id = 1))
#  id M.1 M.2
#1  1   1   3

Is there a way to use an array in a data.frame like I can use a matrix in a 
data.frame?

I am using R version 4.3.0.

Kind regards,
Georg

__
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] Why is R making a copy-on-modification after using str?

2020-11-30 Thread Georg Kindermann
Dear list members,

I was wondering why R is making a copy-on-modification after using str.

m <- matrix(1:12, 3)
tracemem(m)
#[1] "<0x559df861af28>"
dim(m) <- 4:3
m[1,1] <- 0L
m[] <- 12:1
str(m)
# int [1:4, 1:3] 12 11 10 9 8 7 6 5 4 3 ...
dim(m) <- 3:4  #Here after str a copy is made
#tracemem[0x559df861af28 -> 0x559df838e4a8]:
dim(m) <- 3:4
str(m)
# int [1:3, 1:4] 12 11 10 9 8 7 6 5 4 3 ...
dim(m) <- 3:4 #Here again after str a copy
#tracemem[0x559df838e4a8 -> 0x559df82c9d78]:

Also I was wondering why a copy is made when having a Task Callback.

TCB <- addTaskCallback(function(...) TRUE)
m <- matrix(1:12, nrow = 3)
tracemem(m)
#[1] "<0x559dfa79def8>"
dim(m) <- 4:3  #Copy on modification
#tracemem[0x559dfa79def8 -> 0x559dfa8998e8]:
removeTaskCallback(TCB)
#[1] TRUE
dim(m) <- 4:3  #No copy

I am using R version 4.0.3.

Kind regards,
Georg

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