zoukankan      html  css  js  c++  java
  • R语言学习(7)字符串和因子

    字符串和因子

    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

  • 相关阅读:
    python基础简记
    关于cmake找不到库的问题
    【STM32H7教程】第92章 STM32H7的FDCAN总线应用之双FDCAN实现(支持经典CAN)
    《安富莱嵌入式周报》第237期:2021.10.25--2021.10.31
    【STM32H7教程】第91章 STM32H7的FDCAN总线基础知识和HAL库API
    【STM32H7教程】第90章 STM32H7的CAN FD总线之关键知识点整理
    【STM32H7教程】第89章 STM32H7的CAN FD总线基础之前世今生
    embOS推出一个RTOS的革命性功能,支持微秒和CPU时钟周期级分辨率的任务调度和API延迟参数设置
    【不是问题的问题】为什么STM32的Flash地址要设置到0x08000000
    《安富莱嵌入式周报》第236期:2021.10.18--2021.10.24
  • 原文地址:https://www.cnblogs.com/JaniceZD/p/7844464.html
Copyright © 2011-2022 走看看