在最后一列上运行算术运算时,R数据帧将行的值加到列中

时间:2012-08-15 21:26:07

标签: r dataframe

我有以下数据框:关于数据和框架的历史很少。这是第二版本初始数据帧具有基于列标题表示的实际相似度值。基于实际的相似度值,每个都已经通过它们所属的bin来识别,并且我认为bin是实际得分。

    cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard
1             3         0            0           1         1            0       0
2             0         0            5           0         2            2       0
3             1         0            2           0         0            1       0
4             0         0            3           0         1            1       0
5             1         3            1           0         4            0       0
6             0         0            1           0         0            0       0

我想要做的是将每个行值相加并将其保存在jaccard列旁边的一列中,但在求和期间我想检查jaccard的值,这里是我想要做的sudo代码jaccard值:

这是SUDO CODE:

If jaccard.value of that row == 5
   (cosinFcolor + cosinEdge + cosinTexture + histoFcolor + histoEdge + histoTexture) += (jaccard.value of the row * .5)
If jaccard.value of that row == 4
   (cosinFcolor + cosinEdge + cosinTexture + histoFcolor + histoEdge + histoTexture) += (jaccard.value of the row * .4)
If jaccard.value of that row == 3
   (cosinFcolor + cosinEdge + cosinTexture + histoFcolor + histoEdge + histoTexture)+= (jaccard.value of the row * .3)
If jaccard.value of that row == 2
   (cosinFcolor + cosinEdge + cosinTexture + histoFcolor + histoEdge + histoTexture) += (jaccard.value of the row * .2)
If jaccard.value of that row == 1
   (cosinFcolor + cosinEdge + cosinTexture + histoFcolor + histoEdge + histoTexture) += (jaccard.value of the row * .1)
else if jaccard.value of that row == 0
   value in the new column is = -1

完成此操作后,我希望得到如下所示的最终数据框:

    cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard  weightedScore
1             3         0            0           1         1            0       0       -1
2             0         0            5           0         2            2       0       -1
3             1         0            2           0         0            1       0       -1
4             0         0            3           0         1            1       0       -1
5             1         3            1           0         4            0       0       -1
6             0         0            1           0         0            0       0       -1
7             0         0            1           0         0            0       1       1.1

我的初始(第一个数据框架,我已经放置)是通过使用帮助StackOverflow用户跟随R代码生成的:

这是R代码:

single_img_sim_no_title <- single_img_similarity
single_img_sim_no_title$title <- NULL
head(single_img_sim_no_title)
#converting it to bins
sing_img_bins <- apply(single_img_sim_no_title, 2, cut, c(-Inf, seq(0.5, 1, 0.1), Inf), labels=0:6)
sing_img_bins[sing_img_bins=="6"] <- "0"
sing_img_bins <- as.data.frame(sing_img_bins)

0 个答案:

没有答案