测试R中每个受试者的多重复制结果的一致性(Test the consistency of the mult-replicate outcomes for each subject in R)
假设我有这样的结果:
df<-data.frame(id=rep(letters[1:4], each=4), stringsAsFactors=FALSE, test=c(rep(FALSE, 4), rep(c(FALSE, TRUE), 4), rep(TRUE, 4))) id test 1 a FALSE 2 a FALSE 3 a FALSE 4 a FALSE 5 b FALSE 6 b TRUE 7 b FALSE 8 b TRUE 9 c FALSE 10 c TRUE 11 c FALSE 12 c TRUE 13 d TRUE 14 d TRUE 15 d TRUE 16 d TRUE我想看到的是测试结果在每个主题中是否一致。 这样:
id consist 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUE在R中实现这一点的简单方法是什么?
Suppose I have an outcome such like:
df<-data.frame(id=rep(letters[1:4], each=4), stringsAsFactors=FALSE, test=c(rep(FALSE, 4), rep(c(FALSE, TRUE), 4), rep(TRUE, 4))) id test 1 a FALSE 2 a FALSE 3 a FALSE 4 a FALSE 5 b FALSE 6 b TRUE 7 b FALSE 8 b TRUE 9 c FALSE 10 c TRUE 11 c FALSE 12 c TRUE 13 d TRUE 14 d TRUE 15 d TRUE 16 d TRUEWhat I wanted to see is whether the test results were consistent across each subject. Such that:
id consist 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUEWhat is an easy way to realize this in R?
最满意答案
这是一个使用aggregate的方法:
aggregate(test ~ id, data=df, FUN=function(x) min(x) == max(x)) id test 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUE对于每个id,函数检查测试结果的最小值是否等于结果的最大值。
第二种方法是使用diff检查值是否有diff :
aggregate(test ~ id, data=df, FUN=function(x) max(abs(diff(x))) == 0) id test 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUE在这里,取绝对值的最大值来得到差异的大小。
Here is a method using aggregate:
aggregate(test ~ id, data=df, FUN=function(x) min(x) == max(x)) id test 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUEFor each, id, the function checks whether the min of the test results equal the maximum of the results.
A second method is to check if there are any differences in the values using diff:
aggregate(test ~ id, data=df, FUN=function(x) max(abs(diff(x))) == 0) id test 1 a TRUE 2 b FALSE 3 c FALSE 4 d TRUEHere, taking the maximum of the absolute value to get the magnitude of the differences.
更多推荐
发布评论