简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM