ch 14, 2012 1:02 AM
To: William Dunlap
Cc: r-help@r-project.org
Subject: Re: [R] reshaping a dataset for a network
Thank you fro the reply.
I managed to arrive till here, then I would like to have it in matrix
where the $1 $2...$5 are the first column.
Il 3/13/2012 8:15 PM, William Dunlap ha scritto
nal Message-
> From: Marco Guerzoni [mailto:marco.guerz...@unito.it]
> Sent: Wednesday, March 14, 2012 1:02 AM
> To: William Dunlap
> Cc: r-help@r-project.org
> Subject: Re: [R] reshaping a dataset for a network
>
> Thank you fro the reply.
> I managed to arrive till here, t
thank you very much
I had managed with
df <- data.frame(a,b)
m <- lapply(split(df, df$a), function(x) x$b)
n <- max(sapply(m, length))
a <- t(sapply(m, function (x) c(x, rep(NA, n - length(x)
but your solution is much more elegant.
best regards
Marco
Il 3/14/2012 1:54 PM, R. Michael Weyla
You can't have "empty" spots like that in an array. One choice would
be to fill them with NAs:
library(plyr)
do.call(rbind.fill.matrix,lapply(split(b,a), t))
Michael
On Wed, Mar 14, 2012 at 4:01 AM, Marco Guerzoni wrote:
> Thank you fro the reply.
> I managed to arrive till here, then I would l
Thank you fro the reply.
I managed to arrive till here, then I would like to have it in matrix
where the $1 $2...$5 are the first column.
Il 3/13/2012 8:15 PM, William Dunlap ha scritto:
Is the following what you want?
> a<- c(1,2,3,4,4,4,5,5)
> b<- c(11,7,4,9,8,3,12,4)
> split(b,
Is the following what you want?
> a <- c(1,2,3,4,4,4,5,5)
> b <- c(11,7,4,9,8,3,12,4)
> split(b, a)
$1
[1] 11
$2
[1] 7
$3
[1] 4
$4
[1] 9 8 3
$5
[1] 12 4
Note that your df<-cbind(a,b) produces a matrix, not the data.frame
that your df suggests you want. Use df<-da
6 matches
Mail list logo