R Language series: Generating data

Source: Internet
Author: User

R Language series: Generating data(2014-05-04 17:41:57) reproduced
Tags: r language education Category: Biological information

Generate Rule Data
1, use ":", such as x=1:10, note that the method can either increment or decrement, such as Y=10:1
2, SEQ, there are two ways to use: ①seq (start, end, step); ②seq (length=9, From=1, to=5)
SEQ also has a shorthand: seq (x) #相当于1: Length (x), but when length (x) is 0 o'clock, returns an integer (0)
3, C (1,2,8)
4, use Scan (), you can wait for keyboard input. During the input process, you can separate each element with a space, or you can enter one element at a line. Just type in a blank line when you're finished typing.
5, Rep (x,n) #将x重复n次, you can use each limit to repeat the form
Rep (1:3,3)
Rep (1:3,each=3)
6, Sequence (4,9) constructs a vector containing 1:4 and 1:9 total 13 elements
7. GL (k,n,length=,label=) constructs a sequence of factors. K is the number of levels, n is the number of consecutive occurrences of each horizontal, length of the entire sequence, and label as the Factor label.
Example: GL (3,5,length=20,label=c (' A ', ' B ', ' C '))
8, Expand.grid (A=1:3,b=1:4,c=c (' x ', ' y ', ' z ')) constructs a data frame that fully matches each level of each parameter.
9. Paste: This function extracts one element from each parameter each time to form a string, until the parameters of the most elements are taken out, the parameters of the other elements are not enough to be recycled. Multiple parameters can be accepted, and each parameter can include multiple elements. Example: Paste (C ("X", "Y"), 1:10, sep= "")
10, COMBN (x, N) #生成x中取n个元素的所有组合

Common random numbers
Runif (n,min=0,max=1) #uniform, evenly distributed
Rnorm (n,mean=0,sd=1) #Gaussian (normal), normal distribution
Rexp (n,rate=1) #exponential, exponential distribution
Rlnorm (n,meanlog=0,sdlog=1) #lognormal, logarithmic normal distribution

Random sampling
Sample (x, N, Replace=false, prob=)
Sample () #随机排列1:10, no repeat sampling by default
Sample (5) #从1:10 randomly 5, no repeat sampling
x=10:20;    sample (x); #随机排列x
Sample (Replace=true) #从1:10 randomly pumped 10 times to allow repeated sampling
Sample (0:1, replace=true) #100次伯努利试验
Sample (C (0,1), Replace=true, Prob=c (0.1, 0.9)) #分别以0.1 and 0.9 probability decimation 0 and 1

Character cut
Strsplit (x, split, fixed = False, Perl = False)
     #根据split将x分割, if split= "", The x is divided into a single character.
     #默认split为正则表达式, you can use Fixed=true to make exact matches to split
     #当perl =true, Regular expression rules using Perl
     #当分隔符为?, +, {, |, (,) to use ' \ \ ' To remove special meanings

character Connection
paste (letters,collapse= ")     # Connect 26 lowercase letters to a string
     #使用collapse指定的连接符, the result of connecting paste is a string
     #鉴别以下几种情况
paste (Letters[1:5])      #不能连接, output 5 letters: "A" " B "" C "" D "" E "
paste (" A "," B "," C "," D "," E ", sep=")      #输出 "ABCDE", note the difference between the former
paste (1:3,4:6,collapse= '-')      #输出 "1 4-2 5-3 6"
paste ( 1:3,4:6,sep= ', collapse= '-')      #输出 "14-25-36"
paste (C (1:3,4:6), collapse= '- ')      #输出 "1-2-3-4-5-6"

Inline constants in the R language
LETTERS #26个大写字母
Letters #26个小写字母
Month.abb #12个月份的三字母缩写
Month.name #12个月份的全称
Pi #3.14 ...
A subset of letters and months can be taken, for example: Letters[c (1:8)]

R Language series: Generating data

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.