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

Taylor, RB rbt501 at york.ac.uk
Mon Jul 16 15:59:28 CEST 2007


Thank Gabor. This worked for me:

 > v <- scan("C:\\temp\\test.txt", what ="")
Read 5 items
 > is.vector(v)
[1] TRUE
 > class(v)
[1] "character"
 > str(v)
  chr [1:5] "Repens" "Laurifolia" "Repens" "Laurifolia" ...

Cheers,
Rob

Gabor Grothendieck wrote:
> See the comment.
> 
> On 7/16/07, Taylor, RB <rbt501 at york.ac.uk> wrote:
>> Thanks Gabor for your help. Is there a way to avoid this section?
>>
>>  > Lines <- "Repens
>>  > +  Laurifolia
>>  > +      Repens
>>  > +  Laurifolia
>>  > +  Laurifolia
>>  > + "
>>
>> I have simplified the dataset somewhat. The real dataset has over 100
>> entries and it would be rather tedious to have to enter each one 
>> separately.
>>
>> Thanks,
>> Rob Taylor
>>
>> Gabor Grothendieck wrote:
>> > Try this:
>> >
>> >> Lines <- "Repens
>> > +  Laurifolia
>> > +      Repens
>> > +  Laurifolia
>> > +  Laurifolia
>> > + "
>> >> # replace next line with v <- scan("myfile.dat", what = "")
>> >> v <- scan(textConnection(Lines), what = "")
>> > Read 5 items
>> >> is.vector(v)
>> > [1] TRUE
>> >> class(v)
>> > [1] "character"
>> >> str(v)
>> > chr [1:5] "Repens" "Laurifolia" "Repens" "Laurifolia" ...
>> >
>> >
>> > On 7/16/07, Taylor, RB <rbt501 at york.ac.uk> wrote:
>> >> 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
>> >>
>> >> _______________________________________________
>> >> R-sig-Geo mailing list
>> >> R-sig-Geo at stat.math.ethz.ch
>> >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >>
>>
>> -- 
>> 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
>>

-- 
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