【发布时间】:2017-02-24 17:17:38
【问题描述】:
我在 R 中有一个大的 data.frame,其过度简化的版本看起来像这样(真正的 data.frame 在“颜色”列中有 20 种颜色,在“数字”列中有 10 个不同的数字:
Color Number Y
blue 1 5
blue 2 3
blue 3 2
red 1 5
red 2 8
red 3 2
green 1 2
green 2 9
green 3 3
对于“颜色”中的每种颜色,我想通过比较“Y”列的相应值,在“数字”列中的所有数字组合之间应用一个函数。让我们以一个简单的函数为例:
if x >= y, print 1, else print 0 # where x and y represent the first and second values to be compared, respectively
我会将此作为输出 data.frame:
Color Comparison Y
blue 1_vs_2 1
blue 1_vs_3 1
blue 2_vs_1 0
blue 2_vs_3 1
blue 3_vs_1 0
blue 3_vs_2 0
red 1_vs_2 0
red 1_vs_3 1
red 2_vs_1 1
red 2_vs_3 1
red 3_vs_1 0
red 3_vs_2 0
green 1_vs_2 0
green 1_vs_3 0
green 2_vs_1 1
green 2_vs_3 1
green 3_vs_1 1
green 3_vs_2 0
【问题讨论】: