Gang Chen
2011-May-22 21:25 UTC
[R] Convert dataframe with two factors from wide to long format
I know how to convert a simple dataframe from wide to long format with one varying factor. However, for a dataset with two factors like the following, Subj T1_Cond1 T1_Cond2 T2_Cond1 T2_Cond2 1 0.125869 4.108232 1.099392 5.556614 2 1.427940 2.170026 0.120748 1.176353 How to elegantly convert to a long form as Subj Time Cond Value 1 1 1 0.125869 1 1 2 4.108232 1 2 1 1.099392 ... Thanks in advance! Gang [[alternative HTML version deleted]]
David Winsemius
2011-May-22 22:00 UTC
[R] Convert dataframe with two factors from wide to long format
On May 22, 2011, at 5:25 PM, Gang Chen wrote:> I know how to convert a simple dataframe from wide to long format > with one > varying factor. However, for a dataset with two factors like the > following, > > Subj T1_Cond1 T1_Cond2 T2_Cond1 T2_Cond2 > 1 0.125869 4.108232 1.099392 5.556614 > 2 1.427940 2.170026 0.120748 1.176353 > > How to elegantly convert to a long form as > > Subj Time Cond Value > 1 1 1 0.125869 > 1 1 2 4.108232 > 1 2 1 1.099392Assume this is a dataframe named 'tst' require(reshape2) ltest <- melt(tst, id.vars =1) dcast(ltest, ubj+substr(variable, 1,2) + substr(variable, 4,8) ~. ) ubj substr(variable, 1, 2) substr(variable, 4, 8) NA 1 1 T1 Cond1 0.125869 2 1 T1 Cond2 4.108232 3 1 T2 Cond1 1.099392 4 1 T2 Cond2 5.556614 5 2 T1 Cond1 1.427940 6 2 T1 Cond2 2.170026 7 2 T2 Cond1 0.120748 8 2 T2 Cond2 1.176353 dcast(ltest, ubj+substr(variable, 1,2) ~ substr(variable, 4,8) ) ubj substr(variable, 1, 2) Cond1 Cond2 1 1 T1 0.125869 4.108232 2 1 T2 1.099392 5.556614 3 2 T1 1.427940 2.170026 4 2 T2 0.120748 1.176353 The modifications to get it exactly as requested are left to the reader> ... > > Thanks in advance! > > Gang > > [[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.David Winsemius, MD West Hartford, CT
Dennis Murphy
2011-May-22 22:04 UTC
[R] Convert dataframe with two factors from wide to long format
Hi:
library(reshape2)
d1 <- melt(d, id = 'Subj')
d1 <- cbind(d1, colsplit(d1$variable, '_', c('Time',
'Cond')))
d1 <- transform(d1,
Time = substr(Time, 2, 2),
Cond = substr(Cond, 5, 5))[c(1, 4, 5, 3)]
str(d1)
d1
You can decide whether to leave Time and Cond as character or to
convert them to numeric or factor.
HTH,
Dennis
On Sun, May 22, 2011 at 2:25 PM, Gang Chen <gangchen6 at gmail.com>
wrote:> I know how to convert a simple dataframe from wide to long format with one
> varying factor. However, for a dataset with two factors like the following,
>
> Subj T1_Cond1 T1_Cond2 T2_Cond1 T2_Cond2
> ?1 ? 0.125869 ? 4.108232 ? 1.099392 ? 5.556614
> ?2 ? 1.427940 ? 2.170026 ? 0.120748 ? 1.176353
>
> How to elegantly convert to a long form as
>
> Subj ?Time Cond ?Value
> 1 ? ? ? ? 1 ? ? ? ?1 ? ? ? 0.125869
> 1 ? ? ? ? 1 ? ? ? ?2 ? ? ? 4.108232
> 1 ? ? ? ? 2 ? ? ? ?1 ? ? ? 1.099392
> ...
>
> Thanks in advance!
>
> Gang
>
> ? ? ? ?[[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.
>
Maybe Matching Threads
- can I host a Ubuntu / Gentoo repository on a CentOS server?
- How to sync an exact list of files, Including deletes!?
- data.frame to array?
- [PATCH] mkfs: add 'label' optional argument
- Is it really valid to discard externally instantiated functions from a TU when marked inline?