我想在R脚本中创建一个小的查找表,但似乎很难。
lookup <- data.frame(
item=c("aaaa","bbbb","dddd"),
price=c(1.00,2.50,3.65)
)
lookup
以上适用于真正的短名单,但已经是物品和价格已经不对齐,如果我们添加了20个字符描述,那将会更难。是否有一种简单的方法可以将其转换为类似
的内容"Item", "Price", "Description"
"aaaa", 1.00 , "A very nice set of aaas"
"bbbb", 2.00 , "More expensive bbbs"
...
"zzzz", 26.00 , "Zese are the most costly"
答案 0 :(得分:3)
也许是这样的:
lookup <- read.csv(text = '
"Item", "Price", "Description"
"aaaa", 1.00 , "A very nice set of aaas"
"bbbb", 2.00 , "More expensive bbbs"
"zzzz", 26.00 , "Zese are the most costly"
')
最好将其保留为 csv 文件,然后使用read.csv()
读取R,并使格式化输出使用knitr::kable()
函数:
library(knitr)
kable(lookup)
|Item | Price|Description |
|:----|-----:|:------------------------|
|aaaa | 1|A very nice set of aaas |
|bbbb | 2|More expensive bbbs |
|zzzz | 26|Zese are the most costly |