R 将数据帧中的值循环到另一个数据帧中进行操作
对于vocab中的每个wordIDx,我需要计算以下公式: 例如wordIDx=1; 我的价值应该是R 将数据帧中的值循环到另一个数据帧中进行操作,r,loops,dataframe,R,Loops,Dataframe,对于vocab中的每个wordIDx,我需要计算以下公式: 例如wordIDx=1; 我的价值应该是 vocab wordIDx V1 1 archive 2 name 3 atheism 4 resources 5 alt wordIDx newsgroup_ID docIdx word/doc totalwords/doc totalwords/newsgroup wordID/newsgroup P(W_j) 1
vocab
wordIDx V1
1 archive
2 name
3 atheism
4 resources
5 alt
wordIDx newsgroup_ID docIdx word/doc totalwords/doc totalwords/newsgroup wordID/newsgroup P(W_j)
1 1 196 3 1240 47821 2 0.028130269
1 1 47 2 1220 47821 2 0.028130269
2 12 4437 1 702 47490 8 0.8
3 12 4434 1 673 47490 8 0.035051912
5 12 4398 1 53 47490 8 0.4
3 12 4564 11 1539 47490 8 0.035051912
我现在有以下代码:
max(log(0.02813027)+sum(log(2/47821),log(2/47821)))
= -23.73506
classifier_3$ans类似这样的东西,但您确实需要清理列名
classifier_3$ans<- max(log(classifier_3$`P(W_j)`)+ (sum(log(classifier_3$`wordID/newsgroup`/classifier_3$`totalwords/newsgroup`))))
vocab你能通过dput(head(dataframe,10))提供你的样本数据吗?分类器3你可以使用classifier\u3它可以工作。但是,我需要在所有wordID中循环,找到最大值(log(P(w_j))+sum(所有docIDx都有wordID的行)…我不知道如何编辑第三行。感谢您的指导。pmax
是max
的并行版本,它在列上工作。计算您想要的最大值作为中间结果的2列,然后应用pmax。
vocab <- read.table(text = "wordIDx V1
1 archive
2 name
3 atheism
4 resources
5 alt", header = TRUE, stringsAsFactors = FALSE)
classifier_3 <- read.table(text = "wordIDx newsgroup_ID docIdx word/doc totalwords/doc totalwords/newsgroup wordID/newsgroup P(W_j)
1 1 196 3 1240 47821 2 0.028130269
1 1 47 2 1220 47821 2 0.028130269
2 12 4437 1 702 47490 8 0.8
3 12 4434 1 673 47490 8 0.035051912
5 12 4398 1 53 47490 8 0.4
3 12 4564 11 1539 47490 8 0.035051912", header = TRUE, stringsAsFactors = FALSE)
classifier_3 <- classifier_3[!duplicated(classifier_3$wordIDx), ]
classifier_3 <- merge(vocab, classifier_3, by = c("wordIDx"))
classifier_3$ans<- pmax(log(classifier_3$`P.W_j.`)+
(log(classifier_3$`wordID.newsgroup`/classifier_3$`totalwords.newsgroup`) +
# isn't that times 2?
log(classifier_3$`wordID.newsgroup`/classifier_3$`totalwords.newsgroup`)),
log(classifier_3$`wordID.newsgroup`/classifier_3$`totalwords.newsgroup`))