【发布时间】:2020-12-09 21:20:38
【问题描述】:
我正在尝试使用 ngram_asweka 逐行识别字符向量中的 ngram,同时维护项目编号、参与者/控制等数据。我尝试过 tapply 和 sapply 却没有成功。我的数据框有更多列,但基本格式如下所示:
| Item | Phrase |
|---|---|
| 1. | Cats and dogs |
| 2. | birds and bees |
我需要它来输出
| Item | Phrase | Ngram |
|---|---|---|
| 1. | Cats and dogs | cats and dogs |
| 1. | Cats and dogs | cats and |
| 1. | Cats and dogs | and dogs |
| 2. | birds and bees | birds and bees |
| 2. | birds and bees | birds and |
这是我的 ngram 函数
myngram <-function(x) {
x<- ngram_asweka(x, min = 2, max = 5, sep = " ") %>% data.frame()
return(x)
这是我尝试过但不起作用的代码。
x<-tapply(df$phrase, df$ID, myngram) %>% data.frame()
错误代码显示“ngram_asweka (x, min = 2, max = 5, sep = " ") 中的错误:尝试在 SET_STRING_ELT 中设置索引 2/2
感谢您的帮助。
【问题讨论】:
标签: r multiple-columns sapply