R language practice cainiao Study Notes (2)

Source: Internet
Author: User

This section describes the data type and data input of the r language.

Factor

In R language, variables can be attributed to nominal, ordered, and continuous variables,

Nominal type: there are no variables in order. Such as cloudy and sunny weather

Ordered type: There is a sequential relationship, but not a quantitative relationship. Moderate mood

Continuous Type: there are numbers and orders. Of course, the continuity here is not continuous in mathematics, but also includes discrete data.

Nominal and sequential types are called factors in R.

The following describes the factor () function.

Diabetes <-C ("type1", "type2", "type1", "type1") diabetes <-factor (diabetes) # The preceding factor stores this vector as (1, 2, 1, 1), and internally associate 1 = Type 2 = type2.

Ordered = true must be specified in the factor () function.

Status <= C ("poor", "Improved", "excellent", "poor") status <-factor (status, ordered = true) # vector encoding is (3, 2, 1, 3)

But how can we ensure that 1 = poor, 2 = improved, 3 = excelent?

status <- facotr(status, order=TRUE, levels = c("Poor", "Improved","Excellent"))

But what is the difference between an ordered factor and a common factor? See the following program:

Patientid <-C (,) age <-C (,) diabetes <-C ("type1", "type2", "type1", "type1 ") status <-C ("poor", "Improved", "excellent", "poor") diabetes <-factor (diabetes) status <-factor (status, order = true) patientdata <-data. frame (patientid, age, diabetes, status) STR (patientdata) # The following content is output

'Data. framework': 4 obs. of 4 variables:
$ Patientid: NUM 1 2 3 4
$ Age: num 25 34 28 52
$ Diabetes: factor W/2 levels "type1", "type2": 1 2 1
$ Status: Ord. Factor W/3 levels "excellent" <"Improved" <...: 3 2 1 3

Summary (patientdata)
# Output (not aligned)

Patientid age Diabetes status
Min.: 1.00 min.: 25.00 type1: 3 excellent: 1
1st Qu.: 1.75 1st Qu.: 27.25 type2: 1 improved: 1
Median: 2.50 median: 31.00 poor: 2
Mean: 2.50 mean: 34.75
3rd Qu: 3.25 3rd Qu.: 38.50
Max.: 4.00 Max.: 52.00

Diabetes and status show the frequency.

List

Do not underestimate the list. The list in the r language can contain vectors, matrices, data boxes, and actually other lists.

Mylist <-List (object1 ,....)

Mylist <-List (name1 = object1, name2 = object2 ,...)

For example

G <-"My first list" H <-C (25, 26, 18, 39) j <-matrix (1:10, nrow = 5) k <-C ("one", "two", "three") mylist <-List (Title = g, ages = H, J, K) mylist # The following is the running result

$ Title
[1] "My first list"

$ Ages
[1] 25 26 18 39

[[3]
[, 1] [, 2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10

[[4]
[1] "one" "two" "three"

The elements are: String, numeric vector, matrix, and linear vector.

TIPS:

1. There is no scalar in R

2. The subscript of R starts from 1.

3. Variable cannot be declared

 

Data Input

1. input via keyboard

Mydata <-data. frame (age = numeric (0), Gender = character (0), weight = numeric (0) mydata <-Edit (mydata) # Or fix (mydata)

2. Text Files with separated symbols

mydataframe <- read.table(file, header=logical_value, sep=“delimiter", row.names="name")

Here, file is an ASCII text file with delimiters, and header is a logical value indicating whether the first line contains the variable name. SEP is used to specify the delimiter for data splitting, row. names is an optional parameter used to specify one or more variables that represent row identifiers.

Example:

grade <- read.table("studentgrades.csv", header=TRUE,sep=",", row.names="STUDENTID"

 

R language practice cainiao Study Notes (2)

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.