简体   繁体   English

R 中的数字行摘要

[英]summary of row of numbers in R

I just hope to learn how to make a simple statistical summary of the random numbers fra row 1 to 5 in R.我只是希望学习如何对 R 中第 1 到 5 行的随机数进行简单的统计汇总。 (as shown in picture). (如图所示)。

And then assign these rows to a single variable.然后将这些行分配给单个变量。

enter image description here在此处输入图像描述

Hope you can help!希望你能帮忙!

When you type something like 3 on a single line and ask R to "run" it, it doesn't store that anywhere -- it just evaluates it, meaning that it tries to make sense out of whatever you've typed (such as 3 , or 2+1 , or sqrt(9) , all of which would return the same value) and then it more or less evaporates.当您在一行中键入类似3的内容并要求 R “运行”它时,它不会将其存储在任何地方 - 它只是评估它,这意味着它会尝试从您输入的任何内容中理解(例如32+1sqrt(9) ,所有这些都将返回相同的值)然后它或多或少地蒸发了。 You can think of your lines 1 through 5 as behaving like you've used a handheld scientific calculator;您可以将第 1 行到第 5 行视为您使用手持科学计算器的行为; once you type something like 300 / 100 into such a calculator, it just shows you a 3 , and then after you have executed another computation, that 3 is more or less permanently gone.一旦你在这样的计算器中输入300 / 100之类的东西,它只会显示一个3 ,然后在你执行另一个计算之后,那个3或多或少会永久消失。

To do something with your data, you need to do one of two things: either store it into your environment somehow, or to "pipe" your data directly into a useful function.要对您的数据做一些事情,您需要做以下两件事之一:以某种方式将其存储到您的环境中,或者将您的数据直接“管道”到有用的 function 中。

In your question, you used this script:在您的问题中,您使用了以下脚本:

1
3
2
7
6

summary()

I don't think it's possible to repair this strategy in the way that you're hoping -- and if it is possible, it's not quite the "right" approach.我认为不可能以您希望的方式修复此策略-如果可能的话,这不是“正确”的方法。 By typing the numbers on individual lines, you've structured them so that they'll evaluate individually and then evaporate.通过在单独的行上键入数字,您已经对它们进行了结构化,以便它们将单独评估然后消失。 In order to run the summary() function on those numbers, you will need to bind them together inside a single vector somehow, then feed that vector into summary() .为了在这些数字上运行summary() function ,您需要以某种方式将它们绑定在一个向量中,然后将该向量输入到summary()中。 The "store it" approach would be “存储它”的方法是

my_vector <- c(1, 3, 7, 2, 6)
summary(my_vector)

The importance isn't actually the parentheses;重要性实际上不是括号。 it's the function c() , which stands for concatenate , and instructs R to treat those 5 numbers as a collective object (ie a vector).它是 function c() ,代表concatenate ,并指示 R 将这 5 个数字视为一个集合 object (即向量)。 We then pass that single object into my_vector() .然后我们将单个 object 传递给my_vector()

To use the "piping" approach and avoid having to store something in the environment, you can do this instead (requires R 4.1.0+):要使用“管道”方法并避免在环境中存储某些内容,您可以这样做(需要 R 4.1.0+):

c(1, 3, 7, 2, 6) |> summary()

Note again that the use of c() is required, because we need to bind the five numbers together first.再次注意,需要使用c() ,因为我们需要先将五个数字绑定在一起。 If you have an older version of R, you can get a slightly different pipe operator from the magrittr library instead that will work the same way.如果您有旧版本的 R,您可以从magrittr库中获得稍有不同的 pipe 运算符,而不是以相同的方式工作。 The point is that this "binding" part of the process is an essential part that can't be skipped.关键是这个过程的“绑定”部分是不能跳过的重要部分。


Now, the crux of your question: presumably, your data doesn't really look like the example you used.现在,您的问题的症结在于:大概,您的数据看起来并不像您使用的示例。 Most likely, it's in some separate.csv file or something like that;最有可能的是,它位于某个单独的.csv 文件或类似文件中; if not, hopefully it is easy to get it into that format.如果没有,希望它很容易变成那种格式。 Assuming this is true, this means that R will actually be able to do the heavy lifting for you in terms of formatting your data.假设这是真的,这意味着 R 实际上能够在格式化数据方面为您完成繁重的工作。

As a very simple example, let's say I have a plain text file, my_example.txt , whose contents are作为一个非常简单的示例,假设我有一个纯文本文件my_example.txt ,其内容为

1
3
7
2
6

In this case, I can ask R to parse this file for me.在这种情况下,我可以让 R 为我解析这个文件。 Assuming you're using RStudio, the simplest way to do this is to use the File -> Import Dataset part of the GUI.假设您使用的是 RStudio,最简单的方法是使用 GUI 的 File -> Import Dataset 部分。 There are various options dealing with things such as headers, separators, and so forth, but I can't say much meaningful about what you'd need to do there without seeing your actual dataset.有多种选项可以处理诸如标题、分隔符等之类的事情,但是如果没有看到您的实际数据集,我不能说您需要在那里做什么有多大意义。

When I import that file, I notice that it does two things in my R console:当我导入该文件时,我注意到它在我的 R 控制台中做了两件事:

  1. my_example <- read.table(...)
  2. View(my_example)

The first line stores an object (called a "data frame" in this case) in my environment;第一行在我的环境中存储了一个 object(在这种情况下称为“数据帧”); the second shows a nice view of how it's rendered.第二个很好地展示了它的渲染方式。 To get the summary I wanted, I just need to extract the vector of numbers I want, which I see from the view is called V1 , which I can do with summary(my_example$V1) .要获得我想要的摘要,我只需要提取我想要的数字向量,我从视图中看到它称为V1 ,我可以使用summary(my_example$V1)来完成。

This example is probably not helpful for your actual data set, because there are so many variations on the theme here, but the theme itself is important: point R at a file, as it to render an object, then work with that object.这个例子可能对你的实际数据集没有帮助,因为这里的主题有很多变化,但主题本身很重要:将 R 指向一个文件,因为它会渲染一个 object,然后使用该 ZA8CFDE6331BD49EB2AC96F891 That's the approach I'd recommend instead of typing data as lines within an R script, as it's much faster and less error-prone.这是我推荐的方法,而不是在 R 脚本中将数据键入为行,因为它更快且不易出错。

Hopefully this will get you pointed in the right direction in terms of getting your data into R and working with it.希望这能让您在将数据导入 R 并使用它方面指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM