Gundala Viswanath
2008-Jun-23 03:03 UTC
[R] Columnwise Alternate Subsetting of a Data Frame
Hi, Given this data frame:> print(repo.dat)V1 V2 V3 V4 V5 .... (can be more) 1 1007_s_at DDR1 2865.1 2901.3 1978.3 300.2 2 1053_at RFC2 103.6 81.6 108.0 101.3 I want to split them into two data frames yielding: V1 V2 V3 ... etc 1 1007_s_at DDR1 2865.1 1978.3 2 1053_at RFC2 103.6 108.0 (V2 and V3 is obtain from V2 and V4 of original repo.dat) and V1 V2 V3 ...etc 1 1007_s_at DDR1 2901.3 300.2 2 1053_at RFC2 81.6 101.3 (V2 and V3 is obtain from V3 and V5 of original repo.dat) In principle what we desire to do is to: 1. Given original data frame with V1, V2, V3, V4.... 2. Split the data frame into two based on V2,V4,V6 ... (even) and V3, V5, V7 ..(odd) in the original data frame 3. Meanwhile keep row names and V1 in two newly formed data frames. Is there a compact way to do it? - Gundala Viswanath Jakarta - Indonesia
You could do something like: k <- dim(repo.dat)[2] odd <- seq(from=1,to=k,by=2) even <- seq(from=2,to=k,by=2) even<-c(1,even) df1 <- repo.dat[,odd) colnames(df1) <- paste("V",odd,sep="") df2 <- repo.dat[,even) colnames(df2) <- paste("V",even,sep="") --- On Mon, 23/6/08, Gundala Viswanath <gundalav at gmail.com> wrote:> From: Gundala Viswanath <gundalav at gmail.com> > Subject: [R] Columnwise Alternate Subsetting of a Data Frame > To: r-help at stat.math.ethz.ch > Received: Monday, 23 June, 2008, 1:03 PM > Hi, > > Given this data frame: > > > print(repo.dat) > V1 V2 V3 > V4 > V5 .... (can be more) > 1 1007_s_at DDR1 2865.1 2901.3 1978.3 300.2 > 2 1053_at RFC2 103.6 81.6 108.0 > 101.3 > > I want to split them into two data frames yielding: > > V1 V2 V3 > ... etc > 1 1007_s_at DDR1 2865.1 1978.3 > 2 1053_at RFC2 103.6 108.0 > > (V2 and V3 is obtain from V2 and V4 of original repo.dat) > > and > > V1 V2 > V3 ...etc > 1 1007_s_at DDR1 2901.3 300.2 > 2 1053_at RFC2 81.6 101.3 > > > (V2 and V3 is obtain from V3 and V5 of original repo.dat) > > In principle what we desire to do is to: > 1. Given original data frame with V1, V2, V3, V4.... > 2. Split the data frame into two based on V2,V4,V6 ... > (even) and V3, > V5, V7 ..(odd) in the original data frame > 3. Meanwhile keep row names and V1 in two newly formed data > frames. > > Is there a compact way to do it? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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.
Charles C. Berry
2008-Jun-23 03:38 UTC
[R] Columnwise Alternate Subsetting of a Data Frame
See ?split help.search() is (or should be) your friend. HTH, Chuck On Mon, 23 Jun 2008, Gundala Viswanath wrote:> Hi, > > Given this data frame: > >> print(repo.dat) > V1 V2 V3 V4 > V5 .... (can be more) > 1 1007_s_at DDR1 2865.1 2901.3 1978.3 300.2 > 2 1053_at RFC2 103.6 81.6 108.0 101.3 > > I want to split them into two data frames yielding: > > V1 V2 V3 ... etc > 1 1007_s_at DDR1 2865.1 1978.3 > 2 1053_at RFC2 103.6 108.0 > > (V2 and V3 is obtain from V2 and V4 of original repo.dat) > > and > > V1 V2 V3 ...etc > 1 1007_s_at DDR1 2901.3 300.2 > 2 1053_at RFC2 81.6 101.3 > > > (V2 and V3 is obtain from V3 and V5 of original repo.dat) > > In principle what we desire to do is to: > 1. Given original data frame with V1, V2, V3, V4.... > 2. Split the data frame into two based on V2,V4,V6 ... (even) and V3, > V5, V7 ..(odd) in the original data frame > 3. Meanwhile keep row names and V1 in two newly formed data frames. > > Is there a compact way to do it? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901