Introduction to the basics of R language: Data import and description statistics

Source: Internet
Author: User

By writing the Great Wall poem October 30, 2011 Comments Off

First, data import

For beginners, facing a blank command-line window, the first real difficulty may be the import of data. There are many ways to import data, such as fetching from Web pages, obtaining public data sources, and importing text files. For a quick start, it is recommended that beginners take the R language in collaboration with Excel spreadsheet methods. That is, you first read and organize the data you want to work with the more familiar Excel, and then "paste" into R.
For example, we first download the Iris.csv demo data from this address, open it in Excel, select all the samples and then "copy". Enter the following command in the R language:

data=read.table (' clipboard ', T)

The read.table is a common command for R to read external data, t means that the first row is the header information, and the entire data exists in a variable named data. Another convenient way to import is to take advantage of the Rstudio function, and choose "Import DataSet" from the workspace menu.
Second, dataframe operation

After the data is imported into the R language, it is stored as a data frame (dataframe). Dataframe is an R data format that can be thought of as a statistical table, where each row represents a sample point, and each column represents a different property or feature of the sample. The basic operation method that beginners need to Master is Dataframe's editing, extracting and arithmetic.
Although it is recommended that beginners work with the data in Excel, sometimes it is necessary to edit the data in R, the following command gives you the opportunity to modify the data and deposit it into the new variable NewData:

newdata=edit (data)

Another scenario is that we may only focus on part of the data, such as extracting the sepal.width variable data from the sample number 20th to 30th from the original data, because the Sepal.width variable is the 2nd variable, so type the following command at this point:

newdata=data[20:30,2]

If you need to extract the sepal.width variable for all data, the following two commands are equivalent:

newdata=data[,2] Newdata=data$sepal.width

The third case is the need to perform some operations on the data, such as the need to enlarge the sepal.width variables of all the samples 10 times times, we first copy the original data, and then use the $ symbol to extract the operand:

Newdata=data newdata$sepal.width=newdata$sepal.width*10

Iii. Description of statistics

Descriptive statistics is a tool to extract information from a large amount of data, the most common is the summary command, run Summary (data) results are as follows: five sub-sites and mean values are computed for numeric variables, and frequency is calculated for categorical variables.

The mean and standard deviation of the sepal.width variable can also be calculated separately

mean (data$sepal.width) SD (data$sepal.width)

Frequency tables and bar graphs for calculating categorical data species variables

table (data$species) Barplot (table (data$species))

For unary numeric data, it is common practice to draw histograms and box plots to observe their distributions:

hist (data$sepal.width) BoxPlot (data$sepal.width)

For two-dollar numeric data, you can observe the rule by scatter plots

plot (data$sepal.width,sepal.length)

If you need to save the drawing results, we recommend that you use the Plot menu command in Rstudio to select Save Plot as Image

Introduction to the basics of R language: Data import and description statistics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.