在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
最满意答案
你正在使用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
相关问答
更多-
你可以使用std::getline : #include
#include int main() { std::ifstream file("Read.txt"); std::string str; while (std::getline(file, str)) { // Process str } } 还要注意,最好只是使用构造函数中的文件名构造文件流,而不是显式打开(同样的关闭,只需让析构函数完成工作 ... -
如果您可以以某种方式为点对生成公共标识符,则可以按此分组,然后计算距离。 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, you ca ...
-
如果我理解你的话: 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 understood y ...
-
如何在Python中将每行从文件读入列表中(How to read each line from a file into list word by word in Python)[2023-06-04]
您可以使用以下列表理解 list_words = [i.split(',') for i in f] You could use the following list comprehension list_words = [i.split(',') for i in f] -
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 open('o ...
-
如果您的输入有两个连续的行,以'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)[2023-10-14]
也许我错过了一些东西,但你永远不会在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.updat ... -
如何从python文件中读取下一行或下一行的单词?(How to read next word or words till next line from file in python?)[2022-02-05]
你正在使用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 yo ... -
你可以尝试这样做: 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 !! You can ...
-
如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)[2022-02-28]
如果使用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 string. ...