[R-sig-Geo] Converting a dataframe to a character vector

Taylor, RB rbt501 at york.ac.uk
Mon Jul 16 14:45:39 CEST 2007


Hello all,

This isn't quite a spatially related question but perhaps someone can 
help me with this problem?

I have some species name recorded in a tab delim text file. They are all 
just one word long as seen below. I want to put them into a vector for 
use in Spatstat, which as far as I can see means reading the data into a 
data frame and then converting it to a character vector.

So first I read the file into R.

 > df <- read.table("C:\\temp\\test.txt")
 > df
           V1
1     Repens
2 Laurifolia
3     Repens
4 Laurifolia
5 Laurifolia

# just to check it's a data frame
 > is.data.frame(df)
[1] TRUE

#so far so good, now to try to convert to a vector, preserving the 
#species names...
 > v <- as.vector(df)
 > is.vector(v)
[1] FALSE

Why will it not convert it to a character vector automatically? Ok after 
consulting the help file I use the "mode" feature to attempt to force it 
to convert to a character string.

 > v <- as.vector(df, mode = "character")
 > is.vector(v)
[1] TRUE

ok some progress .... but then

 > v
[1] "c(2, 1, 2, 1, 1)"

ok I'm confused at this point. What is it actually telling me? That I 
have a vector with the text string "c(2, 1, 2, 1, 1)" in it?

OK, so to be clear I want a vector that looks like this:
[1] "Repens" "Laurifolia" "Repens" "Laurifolia" "Laurifolia"


Any help would be appreciated.
Thanks,
Rob

-- 
Rob Taylor

University of York
Biology Department
PO Box 373
York
YO10 5YW
United Kingdom

Work +44 (0)1904 32 8557
Mob  +44 (0)777 258 5390




More information about the R-sig-Geo mailing list