[R] Pivot

Rui Barradas ruipbarradas at sapo.pt
Tue Jan 29 13:45:35 CET 2013


Hello,

Try the following.

dat <- read.table(text = "
X         Z
x1        102
x2        102
x2        102
x2        77
x3        23
", header = TRUE)

xt <- xtabs( ~ X + Z, data = dat)


# This creates an object of classes "xtabs", "table".
# If you want a matrix you can do it with

cn <- paste(names(dimnames(xt))[2], dimnames(xt)[[2]], sep = ".")
xt <- cbind(xt)
colnames(xt) <- cn
xt


Hope this helps,

Rui Barradas

Em 28-01-2013 23:48, farnoosh sheikhi escreveu:
> Hi,
>
> I have a data set as follow:
>
> X         Z
> x1        102
> x2        102
> x2        102
> x2        77
> x3        23
>
> I need to pivot this data as follows and assign the values based on frequency of column Z:
>
> X       Z.102   Z.77 Z.23
> x1          1        0        0
> x2          21  0
> x3         00  1
>
> Thanks.
>
> Best,Farnoosh Sheikhi
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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