R语言学习字符串和因子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言学习字符串和因子相关的知识,希望对你有一定的参考价值。

字符串和因子

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

 

以上是关于R语言学习字符串和因子的主要内容,如果未能解决你的问题,请参考以下文章

R语言删除包含缺失值的行并将字符数据列(character)转化为因子列(factor)实战

R语言里面的因子

R 语言数据结构 --1 向量和因子②因子-vector

用R语言做因子分析

R语言实战应用-基于R语言的对应分析

R语言_table()函数与因子变量