简体   繁体   中英

How do I concatenate one word with all elements of a list in R barplot title?

This is the code I'm currently running:

n <- 7
N <- 52
r <- 13
reps <- 1000000
deck <- rep(c('h','d','c','s'), each = r)

diamonds <- rep(NA, length.out = reps)
pos <- sample(x = 1:52, size = 7, replace = FALSE)
for(i in 1:reps) {
  hand <- sample(x = deck, replace = FALSE)[pos]
  diamonds[i] <- sum(ifelse(hand == 'd', 1, 0))
}
barplot(table(diamonds), col = 'red', xlab = '# of diamonds',
        ylab = paste('frequency out of',reps,'trials'),
        main = paste('Positions:',pos[1],pos[2],pos[3],pos[4],
                     pos[5],pos[6],pos[7]))

What I'd really like is to be able to give a title to the barplot with something like the following

barplot(..., main = paste('Positions:',pos))

and have the title say "Positions: p1 p2 p3 p4 p5 p6 p7", where p1,p2,...,p7 are the elements of pos.

For anyone that's interested, this code randomly chooses 7 positions from 52 and then counts the number of diamonds ('d') within those 7 positions after each shuffle of the deck for 1000000 shuffles. Then the empirical distribution of the number of diamonds within those 7 cards is plotted.

Use collapse in paste to collapse the multiple elements in a vector containing the base test and pos ,

paste(c('Positions:', pos), collapse=" ")

Otherwise, when you paste "Positions:" to pos you get the former recycled to the length of pos .

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