zoukankan      html  css  js  c++  java
  • R语言中利用unlist、strsplit拆分提取数据

    1、

    ## 测试数据
    test <- c("200005: Smoker_1","200076: Smoker_2","200087: Smoker_3","200088: Smoker_4","200106: Smoker_5","200107: Non-smoker_6") test list1 <- strsplit(test, split = ":") ## 利用strsplit进行拆分, split 指定分隔符 list1 charac <- unlist(list1) ## 拆分列表 charac top <- charac[seq(1,length(list1),2)] ## 提取每个:前部分 top end <- charac[seq(2,length(list1),2)] ## 后部分 end
    > test <- c("200005: Smoker_1","200076: Smoker_2","200087: Smoker_3","200088: Smoker_4","200106: Smoker_5","200107: Non-smoker_6")
    > test    ## 测试数据
    [1] "200005: Smoker_1"     "200076: Smoker_2"     "200087: Smoker_3"     "200088: Smoker_4"     "200106: Smoker_5"     "200107: Non-smoker_6"
    > list1 <- strsplit(test, split = ":")
    > list1   ## 查看列表
    [[1]]
    [1] "200005"    " Smoker_1"
    
    [[2]]
    [1] "200076"    " Smoker_2"
    
    [[3]]
    [1] "200087"    " Smoker_3"
    
    [[4]]
    [1] "200088"    " Smoker_4"
    
    [[5]]
    [1] "200106"    " Smoker_5"
    
    [[6]]
    [1] "200107"        " Non-smoker_6"
    
    > charac <- unlist(list1)   ## 拆分为字符串
    > charac
     [1] "200005"        " Smoker_1"     "200076"        " Smoker_2"     "200087"        " Smoker_3"     "200088"        " Smoker_4"     "200106"       
    [10] " Smoker_5"     "200107"        " Non-smoker_6"
    > top <- charac[seq(1,length(list1),2)]     ## 提取:前部分
    > top
    [1] "200005" "200076" "200087"
    > end <- charac[seq(2,length(list1),2)]    ## 提取:后部分
    > end
    [1] " Smoker_1" " Smoker_2" " Smoker_3"
    > 
  • 相关阅读:
    【Python】小技巧
    【Python】内置函数清单
    【Python】序列的方法
    【Python】异常处理
    【Python】函数的参数对应
    【Python】函数对象
    redhat7安装jdk1.7报错/home/renqiwei/jdk1.7/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
    redhat更换yum源
    python3降级为python2(linux)
    centos安装apt命令
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15707058.html
Copyright © 2011-2022 走看看