Skip to content

creating a vector from a file

8 messages · heimat los, David Winsemius, Jonathan Daily +1 more

#
On Tue, 2011-05-31 at 15:36 +0200, heimat los wrote:
How is the file formatted? Can you provide a small example?
#
On May 31, 2011, at 10:19 AM, heimat los wrote:

            
Something like this may work:

words <- read.table(file="name.txt", sep="=", header=FALSE)
wvec <- words$V1
names(wvec) <- rownames(words)

Need to change the header argument and the col-name argument to "$" if  
you have a header line.
#
So you need to read a file into R in that format?

Try changing the values in ?read.table. Using the example, I was able
to get the data using:

read.table("clipboard", sep = "=", header = F, colClasses =
c("character", "numeric"))
On Tue, May 31, 2011 at 10:19 AM, heimat los <heimatlos72 at gmail.com> wrote:

  
    
#
On Tue, 2011-05-31 at 16:19 +0200, heimat los wrote:
A CSV might be more universal, but this will do.
OK. Save the above as 'words.txt', then from the R prompt:

words.df <- read.table("words.txt", sep="=")
words.vec <- words.df$V2
names(words.vec) <- words.df$V1

Then use words.vec with the snippets::cloud function. I wasn't able to
install the snippets package and test the cloud function, because I am
still using R 2.13.0-alpha.

read.table returns what R calls a 'data frame'; basically a collection
of records over some number of fields. It's like a matrix but different,
since fields may take values of different types. In the example above,
the data frame returned by read.table has two fields named 'V1' and
'V2', respectively. The R expression 'words.df$V2' references the 'V2'
field of words.df, which is a vector. The last expression sets names for
words.vec, by referencing the 'V1' field of words.df.
#
On May 31, 2011, at 10:38 AM, David Winsemius wrote:

            
Looking at Shotwell answer, I see that I got the column name wrong.  
His is the better answer.