首页
登录 | 注册

《R语言》学习笔记(第4篇)

本节讲“字符串” ,“向量”,“列表”

《R语言》学习笔记(第4篇)

 

> #字符串操作
> a <- "Hello"
> b <- "How"
> c <- "are you?"

> print(paste(a,b,c))
[1] "Hello How are you?"

> print(paste(a,b,c, sep="-"))
[1] "Hello-How-are you?"

> print(paste(a,b,c),sep="",collapse = "")
[1] "Hello How are you?"

《R语言》学习笔记(第4篇)
 

> # Total number of digits displayed. Last digit rounded off.
> result <- format(23.123456789, digits = 9)
> print(result)
[1] "23.1234568"
> 
> # Display numbers in scientific notation.
> result <- format(c(6, 13.14521), scientific = TRUE)
> print(result)
[1] "6.000000e+00" "1.314521e+01"
> 
> # The minimum number of digits to the right of the decimal point.
> result <- format(23.47, nsmall = 5)
> print(result)
[1] "23.47000"
> 
> # Format treats everything as a string.
> result <- format(6)
> print(result)
[1] "6"
> 
> # Numbers are padded with blank in the beginning for width.
> result <- format(13.7, width = 6)
> print(result)
[1] "  13.7"
> 
> # Left justify strings.
> result <- format("Hello", width = 8, justify = "l")
> print(result)
[1] "Hello   "
> 
> # Justfy string with center.
> result <- format("Hello", width = 8, justify = "c")
> print(result)

 

> #计算字符串中的字符数 - nchar()函数
> #此函数计算字符串中包含空格的字符数。
> result <- nchar("count the number of the characters")
> print(result)
[1] 34


> text = (" Count The Number of The CHARACTERS")

> print(toupper(text))
[1] " COUNT THE NUMBER OF THE CHARACTERS"

> print(tolower(text))
[1] " count the number of the characters"


> #提取字符串的一部分 - substring()函数
> #此函数提取字符串的部分。
> text2 <- substring(text,2,21)
> print(text2)
[1] "Count The Number of "
> #注意:这里是从1开始的。

向量

> # Atomic vector of type character.
> print("abc");
[1] "abc"
> 
> # Atomic vector of type double.
> print(12.5)
[1] 12.5
> 
> # Atomic vector of type integer.
> print(63L)
[1] 63
> 
> # Atomic vector of type logical.
> print(TRUE)
[1] TRUE
> 
> # Atomic vector of type complex.
> print(2+3i)
[1] 2+3i
> 
> # Atomic vector of type raw.
> print(charToRaw('hello'))
[1] 68 65 6c 6c 6f


> #多元素向量
> # Creating a sequence from 5 to 13.
> v <- 5:13
> print(v)
[1]  5  6  7  8  9 10 11 12 13
> 
> # Creating a sequence from 6.6 to 12.6.
> v <- 6.6:12.6
> print(v)
[1]  6.6  7.6  8.6  9.6 10.6 11.6 12.6
> 
> # If the final element specified does not belong to the sequence then it is discarded.
> v <- 3.8:11.4
> print(v)
[1]  3.8  4.8  5.8  6.8  7.8  8.8  9.8 10.8
> 
> #使用sequence (Seq.)序列运算符
> print(seq(5,9,by=0.4))
> print(seq(5,9,by=0.4))
 [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0


> #c()函数
> 如果其中一个元素是字符,则非字符值被强制转换为字符类型。
> # The logical and numeric values are converted to characters.
> s <- c('apple','red',5,TRUE)
> print(s)
[1] "apple" "red"   "5"     "TRUE" 
> ss <- c(1,2,3,4)
> print(ss)
[1] 1 2 3 4
> sss <- c(1,2,3,"4")
> print(sss)
[1] "1" "2" "3" "4"


> #访问向量元素
> 使用索引访问向量的元素。 []括号用于建立索引。 索引从位置1开始。在索引中给出负值会丢弃来自result.TRUE,FALSE或0和1的元素,也可用于索引。
> 
> # Accessing vector elements using position.
> t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
> u <- t[c(2,3,6)]
> print(u)
[1] "Mon" "Tue" "Fri"
> 
> # Accessing vector elements using logical indexing.
> v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
> print(v)
[1] "Sun" "Fri"
> 
> # Accessing vector elements using negative indexing.
> x <- t[c(-2,-5)]
> print(x)
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
> 
> u <- t[c(-1,-4,-5)]
> print(u)
[1] "Mon" "Tue" "Fri" "Sat"
> #总结,这里的负数的标签,表示从总的里面剔除这些。
> 
> # Accessing vector elements using 0/1 indexing.
> y <- t[c(0,0,0,0,0,0,1)]
> print(y)
[1] "Sun"


> 向量运算
> 可以添加,减去,相乘或相除两个相同长度的向量,将结果作为向量输出。
> 
> #向量元素回收
> 如果我们对不等长的两个向量应用算术运算,则较短向量的元素被循环以完成操作。
> 
> v1 <- c(3,8,4,5,0,11)
> v2 <- c(4,11)
> # V2 becomes c(4,11,4,11,4,11)
> 
> add.result <- v1+v2
> print(add.result)
[1]  7 19  8 16  4 22
> 
> sub.result <- v1-v2
> print(sub.result)
[1] -1 -3  0 -6 -4  0
> 
> 
> #向量元素排序
> 向量中的元素可以使用sort()函数排序。
错误: unexpected symbol in "向量中的元素可以使用sort()函数排序"
> 
> v <- c(3,8,4,5,0,11, -9, 304)
> 
> # Sort the elements of the vector.
> sort.result <- sort(v)
> print(sort.result)
[1]  -9   0   3   4   5   8  11 304
> 
> # Sort the elements in the reverse order.
> revsort.result <- sort(v, decreasing = TRUE)
> print(revsort.result)
[1] 304  11   8   5   4   3   0  -9
> 
> # Sorting character vectors.
> v <- c("Red","Blue","yellow","violet")
> sort.result <- sort(v)
> print(sort.result)
[1] "Blue"   "Red"    "violet" "yellow"
> 
> # Sorting character vectors in reverse order.
> revsort.result <- sort(v, decreasing = TRUE)
> print(revsort.result)
[1] "yellow" "violet" "Red"    "Blue"  
> 

列表

> #列表是R语言对象,它包含不同类型的元素,如数字,字符串,向量和其中的另一个列表。 列表还可以包含矩阵或函数作为其元素。 列表是使用list()函数创建的。
> list_dat <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
> print(list_dat)
[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1


> #命名列表元素
> 列表元素可以给出名称,并且可以使用这些名称访问它们。
> 
> #create a list containing a vector, a matrix and a list
> list_data <- list(c("Jan","Feb","Mar"),matrix(c(3,9,5,1,-2,8),nrow=2),
+ list("green",12.33))
> 
> #give names to the elements in the list
> names(list_data) <- c("1st Quarter","A_matrix","A Inner list")
> 
> #show the list
> print(list_data)
$`1st Quarter`
[1] "Jan" "Feb" "Mar"

$A_matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

$`A Inner list`
$`A Inner list`[[1]]
[1] "green"

$`A Inner list`[[2]]
[1] 12.33


#对表元素的提取
#美元$是表示,一个数据结构中的某一部分
> print(list_data$A_matrix)
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8


> print(list_data[1])
$`1st Quarter`
[1] "Jan" "Feb" "Mar"

> print(list_data[2])
$A_matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

> print(list_data[3])
$`A Inner list`
$`A Inner list`[[1]]
[1] "green"

$`A Inner list`[[2]]
[1] 12.33


> print(list_data)
$`1st Quarter`
[1] "Jan" "Feb" "Mar"

$A_matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

$`A Inner list`
$`A Inner list`[[1]]
[1] "green"

$`A Inner list`[[2]]
[1] 12.33


> #add a new element at the end of the list
> list_data[4] <- "New element"
> print(list_data[4])
[[1]]
[1] "New element"

> list_data[4] <- NULL
> print(list_data[4])
$<NA>
NULL


> #update the 3rd element
> list_data[3] <-"Updated element"
> print(list_data)
$`1st Quarter`
[1] "Jan" "Feb" "Mar"

$A_matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

$`A Inner list`
[1] "Updated element"

> #合并列表
> #通过将所有列表放在一个list()函数中,您可以将许多列表合并到一个列表中。
> # Create lists.
> list1 <- list(1:5)
> print(list1)
[[1]]
[1] 1 2 3 4 5

> list2 <-list(10:14)
> print(list2)
[[1]]
[1] 10 11 12 13 14

> print(class(list1))
[1] "list"
> print(class(list2))
[1] "list"
> # Convert the lists to vectors.
> v1 <- unlist(list1)
> v2 <- unlist(list2)
> 
> print(v1)
[1] 1 2 3 4 5
> print(v2)
[1] 10 11 12 13 14

> print(class(v1))
[1] "integer"
> print(class(v2))
[1] "integer"

 



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号