Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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中的循环_R - Fatal编程技术网

自动完成R中的循环

自动完成R中的循环,r,R,我试图在R中为以下内容编写一个循环。我有一个数据帧df Reads Counts aaa 1 bbb 20 ccc 3 等等。我想得到一个数据帧,它统计小于或等于某个计数值的读取次数。示例有3次读取,计数cut将值放入存储箱。表统计这些值。cumsum将为您提供所需级别的累计总和 > Counts <- c(1, 20, 3) > cut(Counts, c(0, 10, 100)) ## Or, I guess, cut(Counts, c(0, 1

我试图在R中为以下内容编写一个循环。我有一个数据帧df

Reads Counts
aaa     1
bbb     20
ccc     3
等等。我想得到一个数据帧,它统计小于或等于某个计数值的读取次数。示例有3次读取,计数cut将值放入存储箱。表统计这些值。cumsum将为您提供所需级别的累计总和

> Counts <- c(1, 20, 3)
> cut(Counts, c(0, 10, 100)) ## Or, I guess, cut(Counts, c(0, 1, 10, 100)
[1] (0,10]   (10,100] (0,10]  
Levels: (0,10] (10,100]
> table(cut(Counts, c(0, 10, 100)))

  (0,10] (10,100] 
       2        1 
> cumsum(table(cut(Counts, c(0, 10, 100))))
  (0,10] (10,100] 
       2        3 
将第二个参数更改为cut,以匹配您感兴趣的中断


与cut相似的是间期。

在统计学中,X小于阈值的频率称为经验累积分布函数:简称ecdf

在您的情况下,您需要按n放大以将频率转换为计数

使用睡眠数据:

> nrow(sleep) * ecdf(sleep$extra)(5:-2)
[1] 19 17 14 14 10  6  2  0
也就是说,有19种情况下,额外费用少于5英镑,17英镑少于4英镑,以此类推

在你的情况下,你可能会

nrow(df) * ecdf(df$Counts)(c(100,10,1))

可能您需要使用表格进行剪切,即tablecutdf$Counts,breaks=seq10100,by=10谢谢,这很有用。我可以用某种方式绘制表格的结果吗?绘制..表格。。。是这样的
> nrow(sleep) * ecdf(sleep$extra)(5:-2)
[1] 19 17 14 14 10  6  2  0
nrow(df) * ecdf(df$Counts)(c(100,10,1))