Delphine Fontaine
2008-Jan-08 08:41 UTC
[R] problem when extacting columns of a data frame in a new data frame
Dear R-users, I would like to create a new data frame composed of 2 columns of another data frame. But it does not give me what I want...> casesCNST[1:10,]case X1 X2 X3 X4 expected 1 A1 0 0 0 0 E 2 A2 0 0 0 1 C 3 A3 0 0 0 2 C 4 A4 0 0 0 3 C 5 A5 0 0 0 4 C 6 A6 0 0 1 0 C 7 A7 0 0 1 1 C 8 A8 0 0 1 2 C 9 A9 0 0 1 3 C 10 A10 0 0 1 4 C> expectedCNST <- data.frame (cbind (casesCNST$case, casesCNST$expected))> expectedCNST[1:10,]X1 X2 1 1 4 2 112 3 3 223 3 4 334 3 5 445 3 6 556 3 7 593 3 8 604 3 9 615 3 10 2 3 Why does the variables change ?!? Thanks ! Delphine Delphine Fontaine Statistician Data & Statistics Department - Genexion SA ------------------------------------------------------------ 29, Quai du Mont-Blanc Geneva, CH-1201 Switzerland ------------------------------------------------------------ Office: +41 22 704 32 44 Fax: +41 22 704 32 42 Email: Delphine.Fontaine@genexion.com [[alternative HTML version deleted]]
Dieter Menne
2008-Jan-08 08:50 UTC
[R] problem when extacting columns of a data frame in a new data frame
Delphine Fontaine <delphine.fontaine <at> genexion.com> writes:> I would like to create a new data frame composed of 2 columns of another > data frame. But it does not give me what I want... > > > casesCNST[1:10,] > case X1 X2 X3 X4 expected > 1 A1 0 0 0 0 E > 2 A2 0 0 0 1 C...> > expectedCNST <- data.frame (cbind (casesCNST$case, casesCNST$expected)) > > > expectedCNST[1:10,] > X1 X2 > 1 1 4...... attenu[1:10,] attenu[1:10,c("mag","dist")] Dieter
Emmanuel Charpentier
2008-Jan-08 09:34 UTC
[R] problem when extacting columns of a data frame in a new data frame
Delphine Fontaine a ?crit :> Dear R-users, > > I would like to create a new data frame composed of 2 columns of another > data frame. But it does not give me what I want... > >> casesCNST[1:10,] > case X1 X2 X3 X4 expected > 1 A1 0 0 0 0 E > 2 A2 0 0 0 1 C > 3 A3 0 0 0 2 C > 4 A4 0 0 0 3 C > 5 A5 0 0 0 4 C > 6 A6 0 0 1 0 C > 7 A7 0 0 1 1 C > 8 A8 0 0 1 2 C > 9 A9 0 0 1 3 C > 10 A10 0 0 1 4 C > >> expectedCNST <- data.frame (cbind (casesCNST$case, casesCNST$expected)) > >> expectedCNST[1:10,] > X1 X2 > 1 1 4 > 2 112 3 > 3 223 3 > 4 334 3 > 5 445 3 > 6 556 3 > 7 593 3 > 8 604 3 > 9 615 3 > 10 2 3 > > Why does the variables change ?!?'Cause you build your new data frame from vectors with no name (casesCNST$case is a vector with no name). to keep the original names, you should subset the original data frame, with casesCNST[,c(1,6)] or casesCNST[,c("case","expected")]. HTH, Emmanuel Charpentier