[R] Manipulation of data.frame into an array

Rui Barradas ruipb@rr@d@@ @ending from @@po@pt
Thu May 24 17:51:33 CEST 2018


Hello,

I still don't understand, my code returns each imputation in a separate 
data.frame.

mydata <- split(Imputed, cumsum(c(0, diff(Imputed$Y) != 1)))
mydata[[1]]
#  X1 X2 Y
#1  1  0 1
#2  2  1 2
#3  1  0 3
#4  2  1 4
#5  1  1 5
#6  2  1 6
#7  1  0 7
#8  2  1 8


And mydata[[2]] will be the other imputation.

If this is not what you want, can you please post an example output 
mydata[[1]] from the database you have posted?

Rui Barradas

On 5/24/2018 4:14 PM, Ioanna Ioannou wrote:
> Hello everyone,
> 
> 
> Thank you for this. Nonetheless it is not exactly want i need.
> 
> 
> I need mydata[[1]] to provide the values for all 3 variables (Y, X1 and X2) of the first imputation only. As it stands it returns the whole database.
> 
> Any ideas?
> 
> 
> Best,
> 
> ioanna
> 
> 
> 
> ________________________________
> From: Bert Gunter <bgunter.4567 using gmail.com>
> Sent: 24 May 2018 16:04
> To: Ioanna Ioannou
> Cc: r-help using r-project.org
> Subject: Re: [R] Manipulation of data.frame into an array
> 
> This is one of those instances where a less superficial knowledge of R's technical details comes in really handy.
> 
> What you need to do is convert the data frame to a single (numeric) vector for, e.g. a matrix() call. This can be easily done by noting that a data frame is also a list and using do.call():
> 
> ## imp is the data frame:
> 
> do.call(c,imp)
> 
>   X11  X12  X13  X14  X15  X16  X17  X18  X19 X110 X111 X112 X113 X114
>     1    2    1    2    1    2    1    2    1    2    1    2    1    2
> X115 X116  X21  X22  X23  X24  X25  X26  X27  X28  X29 X210 X211 X212
>     1    2    0    1    0    1    1    1    0    1    0    1    0    1
> X213 X214 X215 X216   Y1   Y2   Y3   Y4   Y5   Y6   Y7   Y8   Y9  Y10
>     1    1    0    1    1    2    3    4    5    6    7    8    1    2
>   Y11  Y12  Y13  Y14  Y15  Y16
>     3    4    5    6    7    8
> 
> So, e.g. for a 3 column matrix:
> 
>> matrix(do.call(c,imp), ncol=3)
>        [,1] [,2] [,3]
>   [1,]    1    0    1
>   [2,]    2    1    2
>   [3,]    1    0    3
>   [4,]    2    1    4
>   [5,]    1    1    5
>   [6,]    2    1    6
>   [7,]    1    0    7
>   [8,]    2    1    8
>   [9,]    1    0    1
> [10,]    2    1    2
> [11,]    1    0    3
> [12,]    2    1    4
> [13,]    1    1    5
> [14,]    2    1    6
> [15,]    1    0    7
> [16,]    2    1    8
> 
> Cheers,
> Bert
> 
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Thu, May 24, 2018 at 7:46 AM, Ioanna Ioannou <ii54250 using msn.com<mailto:ii54250 using msn.com>> wrote:
> Hello everyone,
> 
> 
>   I want to transform a data.frame into an array (lets call it mydata), where: mydata[[1]] is the first imputed dataset...and for each mydata[[d]], the first p columns are covariates X, and the last one is the outcome Y.
> 
> 
> Lets assume a simple data.frame:
> 
> 
> Imputed = data.frame( X1 = c(1,2,1,2,1,2,1,2, 1,2,1,2,1,2,1,2),
> 
>                                            X2 = c(0,1,0,1,1,1,0,1, 0,1,0,1,1,1,0,1),
> 
>                                             Y   = c(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8))
> 
> The first 8 have been obtained by the first imputation and the later 8 by the 2nd.
> 
> 
> Can you help me please?
> 
> 
> Best,
> 
> ioanna
> 
>          [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org<mailto:R-help using 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 using 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.
>



More information about the R-help mailing list