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

字符串和因子

1.字符串

创建字符串

> c("HELLO","WORLD")

[1] "HELLO" "WORLD"

使用paste函数连接字符串

> 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=2)     #width表示限制字符长度

[1] "1,...."

toupper、tolower函数更改大小写

> toupper("hello world!")

[1] "HELLO WORLD!"

> tolower("HELLO WORLD!")

[1] "hello world!"

使用substring、substr截取字符串

使用strsplit分割字符串

2.格式化数字

formatC函数

> pow <- 1:3

> (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)    #保留三个数字

[1] "2.72" "7.39" "20.1"

> formatC(power_of_e,digits=3,width=10)    #在前面添加空格

[1] "      2.72" "      7.39" "      20.1"

> formatC(power_of_e,digits=3,format="e")    #科学计数法

[1] "2.718e+00" "7.389e+00" "2.009e+01"

> formatC(power_of_e,digits=3,flag="+")    +前面加上+

[1] "+2.72" "+7.39" "+20.1"

sprintf函数

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

还有format和prettyNum函数用于格式化数字

3.因子:因子是一个用于存储类别变量的特殊的变量类型,有时像字符串,有时像整数

创建因子

数据框自动创建因子

> (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   male   female male

Levels: female male    #female和male称为因子水平

factor函数创建因子

> gender_char <- c("female","male","male","female","male")

> (gender_factor <- factor(gender_char))

[1] female male   male   female male

Levels: female male

R语言学习(5)-字符串和因子,布布扣,bubuko.com

时间: 2024-08-03 10:29:38

R语言学习(5)-字符串和因子的相关文章

R语言学习笔记

參考:W.N. Venables, D.M. Smith and the R DCT: Introduction to R -- Notes on R: A Programming Environment for Data Analysis and Graphics,2003. http://bayes.math.montana.edu/Rweb/Rnotes/R.html 前言:关于R 在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data

R语言学习(1)

将R作为科学计算器使用 1.例: > 1:5 + 6:10 [1]  7  9 11 13 15 > c(1,3,5,7,9)+c(2,4,6,8,10) [1]  3  7 11 15 19 > median(2:5) [1] 3.5 > 1:10 / 3 [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667 2.0000000 2.3333333 [8] 2.6666667 3.0000000 3.3333333 > 

R语言学习(2)

向量矩阵和数组 1.vector函数可以创建指定类型.长度的矢量 (其结果中的值可以是0,FLASE,空字符串) > vector("numeric",5) [1] 0 0 0 0 0 > vector("complex",6) [1] 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i > vector("logical",6) [1] FALSE FALSE FALSE FALSE FALSE FALSE > 

R语言学习笔记2——绘图

R语言提供了非常强大的图形绘制功能.下面来看一个例子: > dose <- c(20, 30, 40, 45, 60)> drugA <- c(16, 20, 27, 40, 60)> drugB <- c(15, 18, 25, 31, 40) > plot(dose, drugA, type="b") > plot(dose, drugB, type="b") 该例中,我们引入了R语言中第一个绘图函数plot.pl

R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数

遇到了小bug: R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数 看到网上别人的做法,发现了用class(A)和class(B)之后才发现,是因为读入的时候数据的类型不对,A.B的类型并不是matrix,才导致了这个问题. 用as.matrix来变型一下,就OK了. R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数,布布扣,bubuko.com

R语言学习(3)

列表和数据框 1.列表 list函数创建列表 > (a_list <- list(c(1,1,2,5,14,42),month.abb,matrix(c(3,-8,1,-3),nrow=2),asin)) [[1]] [1]  1  1  2  5 14 42 [[2]] [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul&qu

R语言学习(4)-环境和函数

环境和函数 1.环境 使用new.env函数创建环境 > an_environment <- new.env() 向环境中分配变量与列表相同 > an_environment[["pythag"]] <- c(12,15,20,21) > an_environment$root <- polyroot(c(6,-5,1)) > assign("moonday",weekdays(as.Date("1969/07/2

R语言学习笔记 之 可视化地研究参议员相似性

基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份挂怒有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的几个特征,并且要求他们对这个新品牌的每个特征按五分制打分.同时也收集了目标人群的社会经济特征,例如:年龄.性别.种族.住址的邮编以及大概的年收入. 通过这份调查问卷,我们想搞清楚品牌如何吸引不同社会经济特征的人群.最重要的是,我们想要知道这个品牌是否有很大的吸引力.换个角度想这个问题,我们想看看

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

字符串和因子 1.字符串 创建字符串 > c("Hello","World")[1] "Hello" "World" paste( ) 函数连接字符串 > c("Hello","World")[1] "Hello" "World"> paste(c("Hello","Hi"),"W