R语言学习(7)字符串和因子

时间:2021-05-14 01:32:17

字符串和因子

1.字符串

  创建字符串

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

  paste( ) 函数连接字符串

> 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"

  tostring() 函数打印字符串

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

 2.格式化数字

  formatC函数

> 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 = 10)             #前面加上一个空格
[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函数

> 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"

还有format(与formatC用法类似)和prettyNum(格式化非常大或非常小的数字)这里两个函数可格式化数字。

   更改大小写

> toupper("I am the king of the world")
[1] "I AM THE KING OF THE WORLD"
> tolower("HELLO WORLD")
[1] "hello world"

3.因子     因子:用于存储类别变量的特殊的变量类型。

  创建因子             数据框内部自动创建因子

> (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 150 female
4 172 male
5 165 male
6 149 female
7 174 female
8 169 male
9 198 male
10 163 female
> class(heights$gender)
[1] "factor"                                                                     #是一个因子
> heights$gender
[1] female male female male male female female male male
[10] female
Levels: female male
> levels(heights$gender)                                              #female和male被称为因子水平
[1] "female" "male"

  factor函数创建因子

> gender_fac <- c("female","male","female","male","male")
> (gender_char <- factor(gender_fac))
[1] female male female male male
Levels: female male