如何将矢量中的字符插入R中的打印命令

时间:2016-07-19 16:49:01

标签: r

所以我有一个事件列表和一个包含两列,事件标题和事件描述的数据框。如何使用事件标题自动填充事件描述列。例如,一个事件标题是足球,另一个是篮球。有没有根据事件标题写出一个变量字段的通用描述:“在公园出来享受(活动标题)!”

谢谢

1 个答案:

答案 0 :(得分:0)

如果您还想保持事件描述的随机性:

eventsDescriptions <- c("come out and enjoy %s at the park", "its a great day to play %s at the park", "perfect day for a game of %s at the park")

> df <- data.frame(title = c("basketball","soccer","football","rugby","tennis"))
> df
       title
1 basketball
2     soccer
3   football
4      rugby
5     tennis

> df$description <- sprintf(sample(events,size=nrow(df), replace = TRUE), df$title)
> df
       title                                  description
1 basketball    come out and enjoy basketball at the park
2     soccer   its a great day to play soccer at the park
3   football its a great day to play football at the park
4      rugby         come out and enjoy rugby at the park
5     tennis        come out and enjoy tennis at the park

如果您不想随机挑选事件描述,那么就像其中一条评论中提到的那样:

df$description <- sprintf("come out and enjoy %s at the park", df$title)