Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a vector from a file in R

Tags:

r

I am new to R and my question should be trivial. I need to create a word cloud from a txt file containing the words and their occurrence number. For that purposes I am using the snippets package. As it can be seen at the bottom of the link, first I have to create a vector (is that right that words is a vector?) like bellow.

> words <- c(apple=10, pie=14, orange=5, fruit=4)

My problem is to do the same thing but create the vector from a file which would contain words and their occurrence number. I would be very happy if you could give me some hints.

Moreover, to understand the format of the file to be inserted I write the vector words to a file.

> write(words, file="words.txt")

However, the file words.txt contains only the values but not the names(apple, pie etc.).

$ cat words.txt
10 14 5 4

Thanks.

like image 638
heimatlos Avatar asked May 31 '11 14:05

heimatlos


2 Answers

words is a named vector, the distinction is important in the context of the cloud() function if I read the help correctly.

Write the data out correctly to a file:

write.table(words, file = "words.txt")

Create your word occurrence file like the txt file created. When you read it back in to R, you need to do a little manipulation:

> newWords <- read.table("words.txt", header = TRUE)
> newWords
        x
apple  10
pie    14
orange  5
fruit   4
> words <- newWords[,1]
> names(words) <- rownames(newWords)
> words
 apple    pie orange  fruit 
    10     14      5      4

What we are doing here is reading the file into newWords, the subsetting it to take the one and only column (variable), which we store in words. The last step is to take the row names from the file read in and apply them as the "names" on the words vector. We do the last step using the names() function.

like image 71
Gavin Simpson Avatar answered Sep 21 '22 11:09

Gavin Simpson


Yes, 'vector' is the proper term.

EDIT:
A better method than write.table would be to use save() and load():

save(words. file="svwrd.rda")
load(file="svwrd.rda")

The save/load combo preserved all the structure rather than doing coercion. The write.table followed by names()<- is kind of a hassle as you can see in both Gavin's answer here and my answer on rhelp.

Initial answer: Suggest you use as.data.frame to coerce to a dataframe an then write.table() to write to a file.

write.table(as.data.frame(words), file="savew.txt")
saved <- read.table(file="savew.txt")
saved
       words
apple     10
pie       14
orange     5
fruit      4
like image 22
IRTFM Avatar answered Sep 20 '22 11:09

IRTFM