R Language Learning (5)-strings and Factors

Source: Internet
Author: User
String and factor 1. Create a string
> C ("hello", "world") [1] "hello" "world"

Use the paste function to connect to a string
> Paste (C ("hello", "Hi"), "world") [1] "Hello World" "Hi world"
> Paste (C ("hello", "Hi"), "world", SEP = "-") [1] "hello-World" "Hi-World"> paste (C ("hello", "Hi"), "world", collapse = "! ") [1]" Hello world! Hi world "> paste0 (C (" hello "," Hi ")," world ") # The paste0 function can remove the separator [1]" helloworld "" hiworld"

Tostring function print string
> X [1] 1 4 9 16 25> tostring (x) [1] "1, 4, 9, 16, 25"> tostring (x, width = 2) # width indicates the maximum character length [1] "1 ,...."

The toupper and tolower functions change the case sensitivity.
> Toupper ("Hello world! ") [1]" Hello world! "> Tolower (" Hello world! ") [1]" Hello world! "

Use substring and substr to intercept a string
Use strsplit to separate strings

2. formatc Function
> POW <-> (power_of_e <-exp (POW) [1] 2.718282 7.389056 20.085537> formatc (power_of_e) [1] "2.718" "7.389" "20.09"> formatc (power_of_e, digits = 3) # retain three numbers [1] "2.72" "7.39" "20.1"> formatc (power_of_e, digits = 3, width = 10) # Add a space [1] "2.72" "7.39" "20.1"> formatc (power_of_e, digits = 3, format = "e ") # scientific notation [1] "2.718e + 00" "7.389e + 00" "2.009e + 01"> formatc (power_of_e, digits = 3, flag = "+ ") + Add + [1] "+ 2.72" "+ 7.39" "+ 20.1"

Sprintf Function
> Sprintf ("% S % d % E", "Euler's constant to the power", pow, power_of_e) [1] "Euler's constant to the power 1 2.718282e + 00" [2] "Euler's constant to the power 2 7.389056e + 00" [3] "Euler's constant to the power 3 2.008554e + 01"

The format and prettynum functions are also used to format numbers.

3. factor: a factor is a special variable type used to store class variables. Sometimes it is like a string or an integer to create a factor.
Data box automatic factor Creation
> (Heights <-data. frame (height_cm = C (153,181,150,172,165), Gender = C ("female", "male", "male", "female", "male ")))
Height_cm gender
1 153 female
2 181 male
3 150 male
4 172 female
5 165 male
> Class (heights $ gender)
[1] "factor"
> Heights $ gender
[1] Female Male
Levels: Female Male # female and male are called Factor Levels.

Factor Function creation factor
> Gender_char <-C ("female", "male", "male", "female", "male ")
> (Gender_factor <-factor (gender_char ))
[1] Female Male
Levels: Female Male

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.