如何将函数应用于数据帧中所有行的组合? [英] How to apply a function to all combinations of rows in a data frame?
问题描述
我无法解决以下有关下面的(通过限制列数的简化)数据框注释的问题。
I have trouble solving the following problem concerning the (simplified by limiting number of columns) data frame 'annotations' below.
require(irr)
# data
annotations <- read.table(text = "Obj1 Obj2 Obj3
Rater1 a b c
Rater2 a b b
Rater3 a b c", header = TRUE, stringsAsFactors = FALSE)
我想将irr包中的同意函数应用于所有行的组合(而不是排列),结果如下。
I would like to apply the function agree from the irr package to all combinations (not permutations) of rows, resulting in the following.
Agreement rater 1-2: 67%
Agreement rater 1-3: 100%
Agreement rater 2-3: 67%
我需要在行的所有组合上运行一个函数,该函数将需要访问多个/所有列。
I need to run a function on all combinations of rows and the function would need to access a number of/all columns.
我已经解决了部分问题的答案;我已经生成了运行 combn(rownames(annotations),2)
的组合的列表,但是我看不到如何在不编写低效的for循环的情况下使用此列表。
I have worked out parts of the answer to the problem; I have generated a list of combinations running combn(rownames(annotations), 2)
, but I don't see how to use this list without writing inefficient for loops.
我尝试申请,就像 apply(注解,1,同意)
一样,但是我只能使它起作用
I have tried apply, as in apply(annotations, 1, agree)
, but I can only get this to work on one row, not the combinations mentioned before.
有人知道如何进行吗?
更新:根据您的建议,以下解决方案有效。 (我使用了irr软件包中的 kappa2
而不是 agree
,但是主要问题的解决方案仍然相同。 )
UPDATE: The following solution, based on your suggestions, works. (I have used kappa2
from the irr package instead of agree
, but the solution to the main question remains the same.)
require(irr) #require the irr library for agreement calculations
annotations <- read.table(text = "Obj1 Obj2 Obj3
Rater1 a b c
Rater2 a b b
Rater3 a b c
Rater4 c a a", header = TRUE, stringsAsFactors = FALSE)
annotations <- t(annotations) #transpose annotations (rows become columns and vice versa)
kappa_list <- combn(colnames(annotations), 2, FUN=function(x) kappa_list[[length(kappa_list)+1]] = kappa2(matrix(c(annotations[,x[1]], annotations[,x[2]]), ncol=2))$value) #fill kappa_list with all pairs of columns (combinations of 2 raters) in annotations and, per combination, add a value to kappa_list that consists of the value of kappa2 applied to the current combination of raters
kappa_list # display the list of values
推荐答案
您很近,您只需要改为 combn
的结果应用
。我不知道您要指的是什么函数,但是如果插入函数,它应该也能起作用。
You are close, you just need to apply
on the result of combn
instead. I have no idea what function you are referring to, but this should work the same if you plug in your function.
首先,将结果另存为列表,因为添加名称(将两个条目合并在一起)会更容易:
First, save the results as a list instead, because it is easier to add names (which I am adding my combining the two entries together):
toCheck <- combn(rownames(annotations), 2, simplify = FALSE)
names(toCheck) <-
sapply(toCheck, paste, collapse = " - ")
然后,使用 sapply
完成组合。在这里,我使用平均值
进行比较,但是请在此处使用您需要的内容。如果返回的值不止一个,请使用 lapply
然后根据需要打印结果
Then, use sapply
to work through your combinations. Here, I am using mean
to do the comparison, but use what you need here. If you are returning more than a single value, use lapply
then work with the result to print as desired
sapply(toCheck, function(x){
mean(annotations[x[1], ] == annotations[x[2], ])
})
哪个返回:
Rater 1 - Rater 2 Rater 1 - Rater 3 Rater 2 - Rater 3
0.6666667 1.0000000 0.6666667
这篇关于如何将函数应用于数据帧中所有行的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!