Dear Manuel,

This is not a matrix versus data-frame issue. The difference is because 
in the first case you are extracting multiple items at a time, so the 
column names are attached. But in the second, you are really only 
extracting individual items, and so the column names are not necessary.

Here's a simple example:
DET <- as.matrix(finaldf[,3:6]) # Attaches the column names

DET <- as.matrix(finaldf[,3]) # Does not.

DET <- as.matrix(finaldf[,c(3, 5)]) # Also does not.

DET <- as.data.frame(finaldf[,3:6]) # Also attaches the column names

DET <- as.data.framefinaldf[,3]) # Also does not.

DET <- as.data.frame(finaldf[,c(3, 5)]) # Also does not.


Hope this helps,
Phil


On 7/20/2012 11:13 AM, Manuel Spínola wrote:
> Dear list members,
>
> I am using function cbind but I got 2 different results, one is a data
> frame and the other is a matrix and I would like to undertand why is the
> reason:
>
> I have a data frame called "finaldf"
>
>> names(finaldf)
>   [1] "especie" "estrato" "oc1"     "oc2"     "oc3"     "oc4"     "oc5"
> "oc6"
>   [9] "oc7"     "oc8"     "oc9"     "oc10"    "oc11"    "oc12"    "oc13"
>   "oc14"
> [17] "oc15"    "oc16"    "oc17"    "oc18"    "oc19"    "oc20"
>
> I tried (gave me  a data frame):
>
> DET <- cbind(finaldf[, 3:22])
>
>> head(DET)
>     oc1 oc2 oc3 oc4 oc5 oc6 oc7 oc8 oc9 oc10 oc11 oc12 oc13 oc14 oc15 oc16
> oc17 oc18
> 1    0   0   0   0   0   0   0   0   1    0    0    0    0    0    0    0
>   0    0
> 27   0   0   0   1   0   0   0   0   0    0    0    0    0    0    0    1
>   0    0
> 53   0   0   0   0   0   0   0   0   0    0    0    0    0    0    0    0
>   0    0
> 2    1   1   1   1   1   1   0   1   1    1    1    1    0    1    0    1
>   1    1
> 28   1   0   1   0   1   1   1   1   1    1    1    1    0    1    1    1
>   1    0
> 54   1   1   1   1   1   1   1   1   0    0    0    1    1    0    1    0
>   0    0
>     oc19 oc20
> 1     0    0
> 27    1    0
> 53    0    0
> 2     1    1
> 28    1    0
> 54    0    1
>
> and gave me a different results when I tried (gave me a matrix):
>
> DET <-  cbind(finaldf$oc1, finaldf$oc2, finaldf$oc3, finaldf$oc4,
> finaldf$oc5, finaldf$oc6, finaldf$oc7, finaldf$oc8, finaldf$oc9,
> finaldf$oc10, finaldf$oc11, finaldf$oc12, finaldf$oc13, finaldf$oc14,
> finaldf$oc15, finaldf$oc16, finaldf$oc17, finaldf$oc18, finaldf$oc19,
> finaldf$oc20)
>
>> head(DET)
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
> [,14]
> [1,]    0    0    0    0    0    0    0    0    1     0     0     0     0
>    0
> [2,]    0    0    0    1    0    0    0    0    0     0     0     0     0
>    0
> [3,]    0    0    0    0    0    0    0    0    0     0     0     0     0
>    0
> [4,]    1    1    1    1    1    1    0    1    1     1     1     1     0
>    1
> [5,]    1    0    1    0    1    1    1    1    1     1     1     1     0
>    1
> [6,]    1    1    1    1    1    1    1    1    0     0     0     1     1
>    0
>       [,15] [,16] [,17] [,18] [,19] [,20]
> [1,]     0     0     0     0     0     0
> [2,]     0     1     0     0     1     0
> [3,]     0     0     0     0     0     0
> [4,]     0     1     1     1     1     1
> [5,]     1     1     1     0     1     0
> [6,]     1     0     0     0     0     1
>
> Best,
>
> Manuel
>
>
>
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

Reply via email to