我希望根据数据框的某些列生成模式。 样本数据集可以是:
domain <- c('ebay.com','facebook.com','auto.com')
id <- c(21000, 23400, 26800)
cost <- c(0.82,0.40,0.57)
rand_val <- c(0000807,0000808,0000809)
test_data <- data.frame(domain,id,cost,rand_val)
基于test_data的COLUMNS,我希望将此模式生成为文本输出:
if every domain = "ebay.com", id in (21000):
rand_val:0000807
cost: 0.82
elif every domain = "facebook.com", id in (23400):
rand_val:0000808
cost: 0.40
elif every domain = "auto.com", id in (26800):
rand_val:0000809
cost: 0.57
如何根据数据框中的行数进行迭代以生成此模式。
答案 0 :(得分:1)
您可以将paste
与sep
和collapse
一起使用:
res <- paste('if every domain = "',test_data$domain, '", id in (', test_data$id, '):
rand_val:', test_data$rand_val, '
cost: ', test_data$cost, sep="", collapse = "
el")
cat(res)
if every domain = "ebay.com", id in (21000):
rand_val:807
cost: 0.82
elif every domain = "facebook.com", id in (23400):
rand_val:808
cost: 0.4
elif every domain = "auto.com", id in (26800):
rand_val:809
cost: 0.57