我有一個包含2列和多行的數據集。 第一列ID,第二列屬於它的文本。計算R中某個數據幀行的特定詞的出現次數
我想添加更多的列,總結某些字符串在行的文本中出現的次數。該字符串將是 「\ n個正\ n」, 「\ N零\ n」, 「\ n是負面的\ n」`數據集的
例子:
Id, Content
2356, I like cheese.\n Positive\nI don't want to be here.\n Negative\n
3456, I am alone.\n Neutral\n
在最後它看起來應該像
Id, Content,Positiv, Neutral, Negativ
2356, I like cheese.\n Positive\nI don't want to be here.\n Negative\n,1 ,0 ,1
3456, I am alone.\n Neutral\n, 0, 1, 0
現在,我嘗試過這樣的,但它沒有做出正確的回答:
getCount1 <- function(data, keyword)
{
Positive <- str_count(Dataset$CONTENT, keyword)
return(data.frame(data,Positive))
}
Stufe1 <-getCount1(Dataset,'\n Positive\n')
################################################################
getCount2 <- function(data, keyword)
{
Neutral <- str_count(Stufe1$CONTENT, keyword)
return(data.frame(data,Neutral))
}
Stufe2 <-getCount2(Stufe1,'\n Neutral\n')
#####################################################
getCount3 <- function(data, keyword)
{
Negative <- str_count(Stufe2$CONTENT, keyword)
return(data.frame(data,Negative))
}
Stufe3 <-getCount3(Stufe2,'\n Negative\n')
而在這種情況下,比賽應該是零,對吧?查找'gregexpr'和'regmatches'作爲起點。或者,有幾個軟件包可以像「stringr」或「stringi」一樣使用。 – A5C1D2H2I1M1N2O1R2T1
歡迎來到StackOverflow!請閱讀關於[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)以及如何生成[最小可重現示例]的信息(http://stackoverflow.com/questions/5963269 /如何對化妝一個偉大-R-重複性,例如/ 5963610#5963610)。這會讓其他人更容易幫助你。 – Jaap