我想要获取文件类型的最大和最小更改次数。DF:
filef filetypedef dev
[1]/cvsroot/junit/junit/README.html html "egamma"
[2]/cvsroot/junit/junit/README.html html "egamma"
[3]/cvsroot/junit/junit/README.html html "egamma"
[4]/cvsroot/junit/junit/README.html html "egamma"
[5]/cvsroot/junit/junit/README.html html "egamma"
[6]/cvsroot/junit/junit/README.html html "egamma"
[7]/cvsroot/junit/junit/SUPPORT.html html "emeade"
[8]/cvsroot/junit/junit/SUPPORT.html html "emeade"
[9]/cvsroot/junit/junit/SUPPORT.html html "egamma"
[10]/cvsroot/junit/junit/SUPPORT.html html "egamma"
[11]/cvsroot/junit/junit/SUPPORT.html html "emeade"
[12]/cvsroot/junit/junit/build.xml xml "egamma"
[13]/cvsroot/junit/junit/build.xml xml "emeade"
[14]/cvsroot/junit/junit/build.xml xml "emeade"
[15]/cvsroot/junit/junit/build.xml xml "emeade"
[16]/cvsroot/junit/junit/build.xml xml "emeade"
[17]/cvsroot/junit/junit/build.xml xml "emeade"
[18]/cvsroot/junit/junit/new.xml xml "egamma"
[19]/cvsroot/junit/junit/new.xml xml "egamma"
[20]/cvsroot/junit/junit/new.xml xml "egamma"现在他向我展示了每种类型的最大和最小变化,但我希望他也区分文件名。表示数据类型xml最大更改6次,最小更改3次。
我怎样才能做到这一点呢?
这是我的函数
filetype.table <- function(x){ count(filename, filetypedef)
mean <- sort(sapply(table(x$filetypedef),mean), decreasing = TRUE)
num <- sort(sapply(table(x$filetypedef),length), decreasing = TRUE)
min <- sort(sapply(table(x$filetypedef),min), decreasing = TRUE)
max <- sort(sapply(table(x$filetypedef),max), decreasing = TRUE)
rbind(mean, num, min, max)
}
num is the number of different files
min and max is the minimum and maximum number of changes for that file
mean is is the mean number of changes of the filetype目前,他只使用filetype,但我希望它也能使用filef行。例如:他向我展示了每种类型的最大和最小变化,但我希望他也区分不同的文件名。表示数据类型xml最大更改6次,最小更改3次。
输出应如下所示:
html xml
min 5 3
max 6 6
mean 5.5 4.5
num 2 2发布于 2020-05-18 01:15:04
这里的诀窍是将多个列传递给table。
changes = table(df[, c("filef", "filetypedef")])
apply(changes, 2, range) filetypedef
html xml
[1,] 0 0
[2,] 6 6在这里,您的最小值通常为零。看起来您对0不感兴趣,所以可以通过将它们设置为NA来删除它们。
changes[changes==0] = NA
apply(changes, 2, range, na.rm = TRUE)这将给出您的问题中描述的结果。它还可以扩展到任意数量的文件类型。
filetypedef
html xml
[1,] 5 3
[2,] 6 6要添加其他指标(如更新的问题中所示),只需将结果重新绑定到一个矩阵中:
rbind(
mean = apply(changes, 2, mean, na.rm = TRUE),
total = apply(changes, 2, sum, na.rm = TRUE),
min = apply(changes, 2, min, na.rm = TRUE),
max = apply(changes, 2, max, na.rm = TRUE)
) html xml
mean 5.5 4.5
total 11.0 9.0
min 5.0 3.0
max 6.0 6.0注意:此代码仅使用基本R函数(如修订后的问题中所规定的)。
发布于 2020-05-18 01:25:16
我想,[1] - [20]实际上不是您的文件名的一部分,所以我删除了它们。
df %>%
mutate(filename = gsub("\\[[0-9]{1,2}]", "", df$filef)) %>%
count(filename, filetypedef) %>%
group_by(filetypedef) %>%
summarise(min=min(n), max=max(n))这给了我们
# A tibble: 2 x 3
filetypedef min max
<chr> <int> <int>
1 html 5 6
2 xml 3 6https://stackoverflow.com/questions/61854931
复制相似问题