R Language Learning (7) strings and factors

Source: Internet
Author: User
Tags pow sprintf

Strings and factors

1. String

Create a string

> C ("Hello", "World")
[1] "Hello" "World"

Paste () function connection string

> C ("Hello", "World")
[1] "Hello" "World"
> 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") #paste0函数能去掉分隔符
[1] "HelloWorld" "Hiworld"

The ToString () function prints a string

> x
[1] 1 4 9 16 25
> toString (x)
[1] "1, 4, 9, 16, 25"
> toString (x,width = 4)
[1] "1,...."
> toString (x,width = ten) #width表示限制字符长度
[1] "1, 4, ...."

2. Formatting numbers

FORMATC function

> Pow <-1:3
> (powers_of_e <-exp (POW))
[1] 2.718282 7.389056 20.085537
> FORMATC (powers_of_e)
[1] "2.718" "7.389" "20.09"
> formatc (powers_of_e,digits = 3) #指定3个数字

[1] "2.72" "7.39" "20.1"
> formatc (powers_of_e,digits = 3,width = ten) #前面加上一个空格
[1] "2.72" "7.39" "20.1"
> formatc (powers_of_e,digits = 3,format = "E") #科学格式
[1] "2.718e+00" "7.389e+00" "2.009e+01"
> formatc (powers_of_e,digits = 3,flag = "+") #前面加上 +
[1] "+2.72" "+7.39" "+20.1"

sprintf function

> sprintf ("%s%d =%f", "Euler's constant to the power", pow,powers_of_e)
[1] "Euler's constant to the power 1 = 2.718282"
[2] "Euler's constant to the power 2 = 7.389056"
[3] "Euler's constant to the power 3 = 20.085537"

There are also format (similar to FORMATC usages) and prettynum (formatted very large or very small numbers) where two functions can format numbers.

Change case

> ToUpper ("I am the King of the World")
[1] "I AM the KING of the World"
> ToLower ("HELLO World")
[1] "Hello World"

3. Factor factor: A special variable type used to store class variables.

Create factor data frame internal auto create factor

> (Heights <-data.frame (height_cm = C (153,181,150,172,165,149,174,169,198,163), gender = C ("female", "male", " Female "," male "," male "," female "," female "," male "," male "," female ")))
HEIGHT_CM gender
1 153 Female
2 181 Male
3 Female
4 172 Male
5 165 Male
6 149 Female
7 174 Female
8 169 Male
9 198 Male
Ten 163 female
> class (Heights$gender)
[1] "factor"#是一个因子
> Heights$gender
[1] Female male female male male female female male male
[Ten] Female
Levels:female Male
> Levels (Heights$gender)#female和male被称为因子水平
[1] "female" "male"

Factor function Creation factor

> GENDER_FAC <-C ("female", "male", "female", "male", "male")
> (Gender_char <-Factor (GENDER_FAC))
[1] Female male female male male
Levels:female Male

R Language Learning (7) strings and factors

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.