Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中以特殊方式使用tapply_R - Fatal编程技术网

如何在R中以特殊方式使用tapply

如何在R中以特殊方式使用tapply,r,R,有这样一个数据帧 developerf filef 1 egamma /cvsroot/junit/junit/README.html 2 egamma /cvsroot/junit/junit/README.html 3 egamma /cvsroot/junit/junit/README.html 4 egamma /cvsroot/junit/junit/R

有这样一个数据帧

     developerf                      filef          
1      egamma   /cvsroot/junit/junit/README.html 
2      egamma   /cvsroot/junit/junit/README.html 
3      egamma   /cvsroot/junit/junit/README.html 
4      egamma   /cvsroot/junit/junit/README.html 
5      egamma   /cvsroot/junit/junit/README.html 
6      egamma   /cvsroot/junit/junit/README.html
7      egamma   /cvsroot/junit/junit/README.html 
8      egamma   /cvsroot/junit/junit/README.html 
9      egamma   /cvsroot/junit/junit/README.html 
10     egamma   /cvsroot/junit/junit/README.html 
11     egamma   /cvsroot/junit/junit/README.html
12     egamma   /cvsroot/junit/junit/build.xml 
13     egamma   /cvsroot/junit/junit/build.xml 
14     egamma   /cvsroot/junit/junit/build.xml 
15     egamma   /cvsroot/junit/junit/build.xml 
16     emeade   /cvsroot/junit/junit/build.xml 
17     emeade   /cvsroot/junit/junit/build.xml 
18     emeade   /cvsroot/junit/junit/build.xml 
19     emeade   /cvsroot/junit/junit/build.xml 
20     egamma   /cvsroot/junit/junit/build.xml
> 
我已经做了一个函数,它告诉我哪个文件被更改了n次

 before<- sort(table(jupit$filef), decreasing = TRUE)
  t<- table(factor(before,levels = c(1,2,3,5,9,10,11)))

1  2  3  5  9 10 11 
0  0  0  0  1  0  1 

您可以事先对文件列进行分解。
级别=
(此处为
字母
)应为数据的唯一文件,即
唯一(jupit$filef)

dat$file <- factor(dat$file, levels=letters, labels=seq(letters))
或使用
的解决方案

+(colSums(with(dat, table(dev, file))) >= 1)
 # 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
 # 1  0  0  0  1  0  0  0  0  1  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0 

玩具数据:


dat我想你要找的是:

table(factor(tapply(df$developerf, df$filef, function(x) 
                    length(unique(x))), levels = 1:10))

# 1  2  3  4  5  6  7  8  9 10 
# 1  1  0  0  0  0  0  0  0  0 
我们可以将其分解,以了解每一步发生的情况

tapply(df$developerf, df$filef, function(x) length(unique(x)))
提供接触每个文件的唯一开发人员的数量

我们将计数转换为
因子
,将级别从1设置为10

factor(tapply(df$developerf, df$filef, function(x) 
              length(unique(x))), levels = 1:10)
最后,我们使用
table
计算1、2…10个开发人员接触一个文件的次数

table(factor(tapply(df$developerf, df$filef, function(x) 
                   length(unique(x))), levels = 1:10))

@请参阅更新的答案。我创建了一个示例,其中包含了更多的变化。这有意义吗?你的建议给出了每个文件被更改的频率,对吗?我不确定什么是更改的逻辑。如果文件是由任何开发人员生成的,我想您需要
1
,否则需要
0
。请参见编辑。无论如何,请尝试以其他人(或以后您)最清楚的方式编写代码,而不是尽可能多地嵌套命令;)我不知道可以把整个函数放在因子函数中。非常感谢。
tapply(df$developerf, df$filef, function(x) length(unique(x)))
factor(tapply(df$developerf, df$filef, function(x) 
              length(unique(x))), levels = 1:10)
table(factor(tapply(df$developerf, df$filef, function(x) 
                   length(unique(x))), levels = 1:10))