在R中为N,Min / Max,SD,Mean和Median创建一个表格(Create a table for N, Min/Max, SD, Mean, and Median in R)

我对R很新,所以请耐心等待这个基本问题。 我有一个使用data.table包创建的数据集DATA。 我在0到1之间创建了200个随机数,然后做了10000次,最后创建了一个数据表,用于每次迭代的描述性统计。 我的代码看起来像这样:

rndm<-runif(200, min=0, max=1)
reps <- data.table(x=runif(200*10000),iter=rep(1:200,each=10000))
DATA <- reps[,list(mean=mean(rndm),median=median(rndm),sd=sd(rndm),min=min(rndm),
max=max(rndm)),by=iter]

数据看起来像这样:

    Mean    Median     SD    Min    Max
1   0.521    0.499   0.287  0.010  0.998
2   0.511    0.502   0.290  0.009  0.996
.    ...     ... 

等等

我想要做的是创建一个表格,找到累积样本均值的N,平均值,中位数,标准差,最小值和最大值(不是像上面那样每列的N)。 我需要输出看起来像这样:

   N     Mean   Median    SD    Min    Max
 10000  .502     .499    .280  .002   .999

我怎么能做到这一点?


I'm very new to R, so please bear with me on this basic question. I have a dataset, DATA, that I created using the data.table package. I created 200 random numbers between 0 and 1, then did that 10000 times, finally creating a data table for with descriptive statistics for each iteration. My code for it looked like this:

rndm<-runif(200, min=0, max=1)
reps <- data.table(x=runif(200*10000),iter=rep(1:200,each=10000))
DATA <- reps[,list(mean=mean(rndm),median=median(rndm),sd=sd(rndm),min=min(rndm),
max=max(rndm)),by=iter]

The data looks something like this:

    Mean    Median     SD    Min    Max
1   0.521    0.499   0.287  0.010  0.998
2   0.511    0.502   0.290  0.009  0.996
.    ...     ... 

etc.

What I want to do is create a table that finds N, mean, median, standard deviation, minimum, and maximum of the accumulated sample means (not of each column like above). I need the output to look something like this:

   N     Mean   Median    SD    Min    Max
 10000  .502     .499    .280  .002   .999

How can I accomplish this?


原文:https://stackoverflow.com/questions/16342261
2024-03-27 21:03

满意答案

你正在使用f.next(),它将返回下一行。 相反,你需要:

with open('/root/Desktop/test.txt', 'r+') as f:
    for line in f:
        found = line.find('Server version:')
        if found != -1:
            version = line[found+len('Server version:')+1:]
            print version

you are using f.next() which will return the next line. Instead you need:

with open('/root/Desktop/test.txt', 'r+') as f:
    for line in f:
        found = line.find('Server version:')
        if found != -1:
            version = line[found+len('Server version:')+1:]
            print version

相关问答

更多

如何一行一行或一整个文本文件读取?(How to read line by line or a whole text file at once?)

你可以使用std::getline : #include <fstream> #include <string> int main() { std::ifstream file("Read.txt"); std::string str; while (std::getline(file, str)) { // Process str } } 还要注意,最好只是使用构造函数中的文件名构造文件流,而不是显式打开(同样的关闭,只需让析构函...

如何在Pig中的csv文件中读取下一行(how to read next line in csv file in Pig)

如果您可以以某种方式为点对生成公共标识符,则可以按此分组,然后计算距离。 ID, x, y A, 0.0, 0.0 A, 18.6, -11.1 B, 36.1, -21.9 B, 53.7, -32.6 ... Group by将返回类似A, {(0.0, 0.0), (18.6, -11.1)} 。 现在你可以编写一个UDF来调用由计算距离的两个点组成的元组。 If you can somehow generate a common identifier for point pairs, y...

如何在Python中访问文件(.txt)中的下一行(How to access next line in a file(.txt) in Python)

如果我理解你的话: b = open(a, 'r+') for line in b: if line.startswith("E PRAM") and "OOPS: 1" in line: next_line = next(b) # do whatever you need 文件提供了所谓的“迭代器协议”,这就是为什么它们在-loops中工作的原因。 如果需要,您也可以手动调用它们的next功能。 查看PEP-234了解更多详情。 If I underst...

如何在Python中将每行从文件读入列表中(How to read each line from a file into list word by word in Python)

您可以使用以下列表理解 list_words = [i.split(',') for i in f] You could use the following list comprehension list_words = [i.split(',') for i in f]

如何在python文件的每一行上追加某些单词的任一侧的字符?(How do append characters on either side of certain words on every line in a file in python?)

with open('inputfile.txt', 'r') as infile: with open('outfile.txt', 'w') as outfile: for line in infile.readlines(): outfile.write(line.replace('string', y + 'string' + y) with open('inputfile.txt', 'r') as infile: with op...

只有当它以特定单词开头时才打印下一行(print next line only if it begins with a specific word)

如果您的输入有两个连续的行,以'prb'开头,后跟以'rt'开头的行,则跳过它们。 唯一的例外是它们是文件中的前三行。 这是因为for line in f:中的行读取以'prb'开头的第一行, myword = next(f)读取第二行。 因此,在下面的迭代line以'rt'开头。 您可以存储前一行,然后检查两行是否匹配,而不是读取下一行: prev = '' with open('/home/user/Desktop/3rdstep.txt') as f: for line in f: ...

Python - 仅在特定情况下读取文件的最后一行(Python - Only reading last line of file in specific circumstance)

也许我错过了一些东西,但你永远不会在for循环中使用line: for line in f: # Update the counter count_all.update(terms_bigram) 所以你只是在为每一行做同样的事情。 Perhaps I am missing something but you never use line in the for-loop: for line in f: # Update the counter count_all....

如何从python文件中读取下一行或下一行的单词?(How to read next word or words till next line from file in python?)

你正在使用f.next(),它将返回下一行。 相反,你需要: with open('/root/Desktop/test.txt', 'r+') as f: for line in f: found = line.find('Server version:') if found != -1: version = line[found+len('Server version:')+1:] print versio...

从文件中读取单词并将其写入同一行(Reading Words from a File and Writing Them on The Same Line)

你可以尝试这样做: with open("infile.txt", "r") as infile: string = infile.read().split("\n\n") with open("outfile.txt", "w") as outfile: for s in string: outfile.write(s.replace("\n"," ") + "\n") 写在文件上的输出: This is a sample input Hello World !! Yo...

如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)

如果使用rstrip()它将删除所有空格以及新行( \n )。 因此,请使用rstrip('\n')仅删除换行符。 当你想循环它时,将逻辑放在for循环中。 f = open('words.txt') for line in f.readlines(): secret = line.rstrip('\n') if (secret[-1:] == "\n"): print "Error, new line character at the end of the st...

相关文章

更多

Create a Bootable MicroSD Card

http://gumstix.org/create-a-bootable-microsd-card.h ...

nutch与起点R3集成之笔记(四)

通过“nutch与起点R3集成之笔记(一、二、三)”中的步骤,我们可以建立起一个行业内部网的搜索引擎, ...

nutch与起点R3集成之笔记(二)

在nutch与起点R3集成之笔记(一)中介绍了在起点R3中添加nutch要用到的索引字段,上述字段建好 ...

Storm常见模式——求TOP N

Storm的另一种常见模式是对流式数据进行所谓“streaming top N”的计算,它的特点是持续 ...

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

在Hadoop集群上运行R程序--安装RHadoop

Hadoop是由Revolution Analytics发起的一个开源项目,它可以将统计语言R与Had ...

Guava集合工具类-Table接口映射处理

System.out.println("Emp&nbsp

hibernate 1+N 问题

有一个表ID表 @Entity @Table(name=&quot;WF_ID_CH_TBL&qu ...

Guava学习笔记:Guava新集合-Table等

  Table   当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map&lt; ...

最新问答

更多

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)

它创建了这些视图: 'auth/login.blade.php', 'auth/register.blade.php', 'auth/passwords/email.blade.php', 'auth/passwords/reset.blade.php', 'layouts/app.blade.php', 'home.blade.php' 并修改这些文件: 'Http/Controllers/HomeController.php', 'routes/web.php' 如果使用--views

如何交换返回集中的行?(How to swap rows in a return set?)

您可以使用特殊的CASE表达式进行ORDER BY如下所示: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END You can ORDER BY using a special CASE expression like this: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END

在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)

我建议你制作一个新的TableView Cell,在这个newTableViewCell中,加载第2节中的所有单元格,然后为newTableViewCell提供边框。 但如果您不想这样做,那么您可以使用以下代码: @property(strong,nonatomic) CAShapeLayer* borderPath; -(void)viewDidLayoutSubviews{ [_borderPath removeFromSuperlayer]; UIView *viewToGiveBord

使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)

出于一些奇怪的原因,我现在发现了一个不同的问题, Boost.Spirit SQL语法/词法分析失败 ,其中提供了一些其他解决方案来进行空格跳过。 一个更好的! 以下是根据建议重新编写的示例代码: #include #include #include #include #include #incl

Java中的不可变类(Immutable class in Java)

1.如果我只有final变量的课程? 这会让你远离但不是全部。 这些变量的类型也需要是不可变的。 考虑一下 class MyImmutableClass { // final variable, referring to a mutable type final String[] arr = { "hello" }; // ... } 这允许有人做 myImmutableObject.arr[0] = "world"; 并有效地改变你的不可变类的对象。 此外,建议禁

WordPress发布查询(WordPress post query)

如果你想要在div中包含所有帖子: post_parent) { $myposts = get_posts('numberposts=10&tag=medical&order=DESC'); echo ' '; foreach ($myposts as $post): ?>

如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)

我不确定哪个是MySQL的正确答案,因为它本身还不支持IPv6地址格式(尽管“ WL#798:MySQL IPv6支持 ”表明它将在MySQL v6.0中,当前文档不支持)。 不过,你建议的人建议去2 * BIGINT,但要确保他们是UNSIGNED。 在IPv6的/ 64地址边界处有一种自然分割(因为/ 64是最小的网格块大小),这将与其很好地对齐。 I'm not sure which is the right answer for MySQL given that it doesn't y

是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)

您可以使用Object.keys和Array#find来获取匹配value的key 。 const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'}; function sw