[R] Speeding up casting a dataframe from long to wide format
Daren Tan
daren76 at hotmail.com
Wed Dec 3 05:52:02 CET 2008
Hi,
I am casting a dataframe from long to wide format. The same codes that works for a smaller dataframe would take a long time (more than two hours and still running) for a longer dataframe of 2495227 rows and ten different predictors. How to make it more efficient ?
wer <- data.frame(Name=c(1:5, 4:5), Type=c(letters[1:5], letters[4:5]), Predictor=c("A", "A", "A", "A", "A", "B", "B"))
> wer
Name Type Predictor
1 1 a A
2 2 b A
3 3 c A
4 4 d A
5 5 e A
6 4 d B
7 5 e B
wer.melt <- melt(wer, id.var=c("Name", "Type"))
cast(wer.melt, Name + Type ~ value, length, fill=0)
Name Type A B
1 1 a 1 0
2 2 b 1 0
3 3 c 1 0
4 4 d 1 1
5 5 e 1 1
> sessionInfo()
R version 2.7.0 (2008-04-22)
x86_64-unknown-linux-gnu
locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape_0.8.0
More information about the R-help
mailing list