【发布时间】:2018-06-07 17:22:02
【问题描述】:
我有一个 10 行的关于 potatoes 的推文数据框,需要根据每个 tweet 包含的标点符号(问号或感叹号)标记它们。 grep 函数将返回出现这些字符的行号:
grep("\\?", potatoes$tweet)
grep("!", potatoes$tweet)
我尝试在 dplyr 中使用mutate 创建标志变量question,如图所示...
potatoes$question <- NA
potatoes <- mutate(potatoes, question = +row_number(grep("\\?", potatoes$tweet)))
Error in mutate_impl(.data, dots) :
Column `question` must be length 10 (the number of rows) or one, not 3
我也很乐意考虑比以grep 的输出为条件的更优雅的解决方案。任何帮助表示赞赏!
【问题讨论】:
标签: r if-statement data-manipulation