以下示例数据:
keyword <- c("advertising plan","advertising budget",
"marketing plan",
"marketing budget",
"hr plan",
"hr budget",
"operation plan",
"operation budget")
indicator <- c(1,0,1,0,0,1,1,1)
df <- cbind(keyword,indicator)
我需要创建一个新变量“ Topic
”。如果关键字为“Advertising
”和“advertising plan
”,我会将单词“advertising budget
”分配给单元格;如果关键字为“Marketing
”和“marketing plan
”,则“marketing budget
”到单元格。
我尝试使用
df$Topic[which(df$keyword == c("advertising plan","advertising budget") <- "Advertising"
。
但它没有成功。任何帮助都非常感谢!
答案 0 :(得分:1)
#Convert to dataframe if it isn't already
df = as.data.frame(df, stringsAsFactors = FALSE)
#Extract first word of the keyword as the topic
df$Topic = sapply(strsplit(df$keyword," "),function(x) x[1])
#Set all values other than marketing and advertising to blank (or NA if you want)
df$Topic[df$Topic != "marketing" & df$Topic != "advertising"] = ""
答案 1 :(得分:1)
keyword <- c("advertising plan","advertising budget",
"marketing plan",
"marketing budget",
"hr plan",
"hr budget",
"operation plan",
"operation budget")
indicator <- c(1,0,1,0,0,1,1,1)
df <- data.frame(keyword, indicator, stringsAsFactors = FALSE)
df[df$keyword %in% c("advertising plan", "advertising budget"), "Topic"] = "Advertising"
df[df$keyword %in% c("marketing plan", "marketing budget"), "Topic"] = "Marketing"
或更灵活的方法:
df[grepl("advertising plan|advertising budget", df$keyword), "Topic"] = "Advertising"
df[grepl("marketing plan|marketing budget", df$keyword), "Topic"] = "Marketing"