R language Notes-vector manipulation

Source: Internet
Author: User
Tags numeric numeric value sort true true

1. The basic elements of the vector are: numeric value (numeric), character (character), logical value (logical), plural type (complex)

2. Vectors do not require a defined type and can be assigned directly:

A<-c (1,2,3,4,5,-3,-4); a #数值型向量

[1] 1 2 3 4 5-3-4

B<-c ("One", "one", "one", "three"); b #字符型向量

[1] "one" "" "" Three "

C<-c (True,true,true,false); C #逻辑型向量

[1] True True True FALSE

X<-c (5,4,2); x

[1] 5 4 2

X[9]<-9;x # when Vector x is not long enough, specify a 9th element of 9

[1] 5 4 2 na na na na na 9 #自动将中间的元素补为NA

3. The vector element subscript value is starting at 1, for example: to take the x vector in the previous value is 1, then x[2]

If there is a character in a vector, the type of the vector becomes a character;

Demo_1<-c (, ' a ')

Mode (demo_1)

[1] "character"

4. Add a name to the vector:

Names (years) <-c ("Kennedy", "Johnson", "Carter", "Clinton"); years

Kennedy Johnson Carter Clinton

1960 1964 1976 1994

5. Addition and merging of vector elements

V<-c (a)

V<-c (v,55) #格式为新向量 <-(original vector, new element)

V

[1] 1 2 3 55

V1 <-C (1, 2, 3)

V2 <-C (4, 5, 6)

C (V1,V2)

[1] 1 2 3 4 5 6

>v<-c (1,2,3,3,3,4)

> Append (v,10,after=3) #在第3个向量后面加入10

[1] 1 2 3 10 3 3 4

6. If the logical variable is combined with a numeric value, it is converted to a numeric value. True turns into 1 and FALSE to 0.

Demo_2<-c (1,2,true)

Mode (demo_2) [1] "Numeric"

DEMO_2[1] 1 2 1

There is no single integer, single character concept in the R language

For example: x<-2;x<-' a '; R is all treated as a vector, except that the vector includes only a single value.

7. Generation of several special vectors

7.1 Generating Series seq ()

Seq (length=, from=, to=)

Length: Specify the number of builds

From: Refers to the point at which the build starts

To: Cutoff point

If not specified, the default condition: seq (n1,n2,by=)

N1: Start position

N2: Cut-off position

by= Specify interval

Seq (length=10,from=10,to=100)

[1] 10 20 30 40 50 60 70 80 90 100

Seq (10,100,10)

[1] 10 20 30 40 50 60 70 80 90 100

7.2 Rep (p,n) repeatedly generates P-value N times

Rep (1,10) [1] 1 1 1 1 1 1 1 1 1 1

8. Vector operations

Take the sub-vector: by subscript implementation

Take an element: X[2];(if x includes a name, note: x[2] and x[[2]];

Take a few: X[c (1,2,0)]

Take one/several elements, use-:x[-n]

9. Vector sorting

Sort (); The output is sorted after the result;

Order (); the position of each vector after the output is sorted;

A<-c (3,9,0,12,19)

Sort (a); order (a);

A<-c (3,9,0,12,19)

Order (a) [1] 3 1 2 4 5

Sort (a) [1] 0 3 9 12 19

10. Add. If two vectors are not the same during the two vectors, R automatically complements the vector with a small length until the length equals the large vector.

For example:

demo_5<-1:3;

demo_6<-2:5;

demo_7= demo_6+ demo_5;

Warning message:

In Demo_6 + demo_5:

Longer object length is not a multiple of shorter object length

demo_5<-1:3;

demo_6<-2:7;

demo_7= demo_6+ demo_5;

DEMO_7[1] 3 5 7 6 8 10

11. Extracting elements of a vector by condition

X<-c (10:1); x

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

X[x==3]<-25;x #将x中等于3的元素变为25

[1] 10 9 8 7 6 5 4 25 2 1

X[x=1]<-12;x #将x的第1个元素数值变为12

[1] 12 9 8 7 6 5 4 25 2 1

> letters[1:30] # Generating alphabetic sequences

[1]&NB

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.