another option is to use reshape()
x<-data.frame(x1=rep(1:3,each=3),x2=letters[1:9])
x$id<-rep(1:3,3)
dur<-reshape(x,timevar="x1",idvar="id",direction="wide")
dur<-dur[,-1]
colnames(dur) <- paste("d", unique(x$x1), sep="")
dur
cheers, Victor
___
Hi André,
try this:
df1 <- data.frame(x1 = rep(1:3, each=3), x2=letters[1:9])
dfs <- split(df1, df1$x1)
df2 <- data.frame(sapply(dfs, FUN="[[", "x2"))
colnames(df2) <- paste("d", unique(df1$x1), sep="")
df2
HTH
Patrick
Am 06.02.2011 12:13, schrieb André de Boer:
Hello,
Can someone give me
Here is one way of doing it:
> x
x1 x2 row
1 1 a 1
2 1 b 2
3 1 c 3
4 2 d 1
5 2 e 2
6 2 f 3
7 3 g 1
8 3 h 2
9 3 i 3
> # create indices for new table
> x$row <- ave(x$x1, x$x1, FUN=seq_along)
> # create output matrix
> result <- matrix('', max(x$row), max(x$x1))
Hello,
Can someone give me hint to change a data.frame.
I want to split a column in more columns depending on the value of a other
column.
Thanks for the reaction,
Andre
Example:
> dat
x1 x2
1 1 a
2 1 b
3 1 c
4 2 d
5 2 e
6 2 f
7 3 g
8 3 h
9 3 i
in
> dur
d1 d2 d3
1 a d g
4 matches
Mail list logo