[R] creating vectors from data-frames

David Winsemius dwinsemius at comcast.net
Mon Jan 9 02:33:56 CET 2012


On Jan 8, 2012, at 3:12 PM, Philip Robinson wrote:

>
>
> I am having a problem with creating a vector from a rows or columns, I
> searched around and found as.vector(x), but it does not seem to do  
> what it
> says it does
>
> I have included an example below, of doing what would seem to be the  
> method
> required to create a vector, but instead it creates a one row data  
> frame.
> What is required to actually create a vector.
>
> Many thanks
>
> Philip
>
>> data
>
Ugh. Bad name ofr data-object.

> V1   V2   V3   V4   V5   V6    V7    V8    V9   V10   V11
>
> 1  E 2369 2304 2312 2460 2645  3038  3265  3760  3904  4421
>
> 2 NZ  705  817  907  917  954 1,026 1,065 1,125 1,276 1,449

The fact that any column has a comma in the output implies that it is  
a character or a factor column.
>
>
>
>> nz <-as.vector(data[2,2:11])

You could use unlist() or c() but I am guessing that your data entry  
might have results in strange consequences:

data <- read.table(text=" V1   V2   V3   V4   V5   V6    V7    V8     
V9   V10   V11
1  E 2369 2304 2312 2460 2645  3038  3265  3760  3904  4421
2 NZ  705  817  907  917  954 1,026 1,065 1,125 1,276 1,449",  
header=TRUE)

 > unlist(data[2,])
  V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11
   2 705 817 907 917 954   1   1   1   1   1

You have not offered dput() on the head of that dataframe. You would  
be well advised to do so now.

>
>
>
>> nz
>
>   V2  V3  V4  V5  V6    V7    V8    V9   V10   V11
>
> 2 705 817 907 917 954 1,026 1,065 1,125 1,276 1,449
>
>
>> class(nz)
>
>
> [1] "data.frame"
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list