简体   繁体   English

如何选择条件并根据条件分配新值

[英]How to select conditions and assign a new value based on the condition

Let's say I have the following data frame with a series of keywords, and I am trying to create tags for each keyword. 假设我有一个带有一系列关键字的以下数据框,并且我正在尝试为每个关键字创建标签。

    keywords = data.frame(keyword=c("aaa auto insurance","cheap car insurance",
"affordable auto insurance","fast insurance quotes","cheap insurance rates",
"Geico insurance","State Farm insurance quote"))

I want to generate a new column called tag, which will look as follows. 我想生成一个名为tag的新列,其外观如下。

keyword                 Tag
aaa auto insurance      brand | auto
cheap car insurance     cheap | car
Geico insurance         brand

I've figured out how to create tags in different column, but was wondering how I could add it all in one column with a single delineator ("|"). 我已经弄清楚了如何在不同的列中创建标签,但是想知道如何用单个定界符(“ |”)将它们全部添加到一个列中。

So here is what I did to generate separate tag columns. 因此,这就是我生成单独的标记列的操作。 I'm wondering how I can alter this code in order to produce what I mentioned previously. 我想知道如何更改此代码以产生我之前提到的内容。

main <- function(df) {
    brand <- c("aaa","State Farm","Geico","Progressive")
    cheap = c("cheap","cheapest")
    affordable=c("affordable")
    auto=c("auto")
    car=c("car")
    quote=c("quote","quotes")
    rate=c("rate","rates")
    for(i in 1:nrow(df)) {
        words = strsplit(as.character(df[i, 'keyword']), " ")[[1]]
        if(any(brand %in% words)){
              df[i, 'brand'] <- 1 }
        else{
              df[i, 'brand'] <- "NULL" }
        if(any(cheap %in% words)){
              df[i, 'cheap'] <- 2 }
        else{
              df[i, 'cheap'] <- "NULL" }
        if(any(affordable %in% words)){
              df[i, 'affordable'] <- 3 }
        else{
              df[i, 'affordable'] <- "NULL" }
        if(any(auto %in% words)){
              df[i, 'auto'] <- 4 }
        else{
              df[i, 'auto'] <- "NULL" }
        if(any(car %in% words)){
              df[i, 'car'] <- 5 }
        else{
              df[i, 'car'] <- "NULL" }
        if(any(quote %in% words)){
              df[i, 'quote'] <- 6 }
        else{
              df[i, 'quote'] <- "NULL" }
        if(any(rate %in% words)){
              df[i, 'rate'] <- 7 }
        else{
              df[i, 'rate'] <- "NULL" }
   }
  return(df)
}

main(keywords)

If you're wondering why the tags have 1:7 values, it's because they're unique to a specific tag. 如果您想知道为什么标记具有1:7的值,那是因为它们对于特定标记是唯一的。

tags = data.frame(id=c(1,2,3,4,5,6,7), tag=c("brand","cheap","affordable","auto","car","quote","rate"))
tags

You can use paste() to concatenate strings. 您可以使用paste()连接字符串。 So you can do something along the lines of 因此,您可以按照以下方式进行操作

df[i, 'tags'] <- paste(df[i, 'tags'], "new-tag", sep="|");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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