如何计算R图中标识线(x = y)上方和下方的点数

时间:2016-05-31 11:25:06

标签: r plot

我试图计算在图中标识线(x = y)下方和上方的点数。

plot(datatumor$ageChronologique, datatumor$ageMethylation, 
     xlab ="Chronological age", ylab= "DNAm age")
abline(0,1)

" ageChronologique"和" ageMethylation"是datatumor的两个测量变量。

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以将逻辑datatumor$ageChronologique < datatumor$ageMethylationsum结合使用:

# above 45 degree line:
sum(datatumor$ageChronologique < datatumor$ageMethylation)
# below 45 degree line:
sum(datatumor$ageChronologique > datatumor$ageMethylation)

使用with可能看起来更好一些:

# above 45 degree line:
with(datatumor, sum(ageChronologique < ageMethylation))

如果变量中缺少值(NAs),可以在sum中包含na.rm = T参数以忽略这些:

sum(datatumor$ageChronologique > datatumor$ageMethylation, na.rm=T)