[R] problem when extacting columns of a data frame in a new data frame

Emmanuel Charpentier charpent at bacbuc.dyndns.org
Tue Jan 8 10:34:02 CET 2008


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




More information about the R-help mailing list