Anita
Anita

Reputation: 789

R: paste element of a vector

I've got the following situation in R:

> wordfreq 
  filiaalnummer       filiaal          type  omschrijving   filiaalnaam    
              1            3             3             2             1  
> names(wordfreq)  
 [1] "filiaalnummer" "filiaal"       "type"          "omschrijving" 
 [5] "filiaalnaam" 

> as.numeric(wordfreq)
 [1] 1 3 3 2 1

Where each time the number represents the frequency of the word above. I would like to paste the names of the vector into one element with the correct frequency, so I would like to get the following:

filiaalnummer filiaal filiaal filiaal filiaal type type type omschrijving omschrijving filiaalnaam

Upvotes: 2

Views: 75

Answers (1)

tchakravarty
tchakravarty

Reputation: 10954

Try this:

rep(names(wordfreq), times = wordfreq)

Here is a reproducible example:

wordfreq = sample.int(10, 5, replace = TRUE)
names(wordfreq) = letters[1:5]
rep(names(wordfreq), times = wordfreq)

Upvotes: 5

Related Questions