楼主: dxystata
4000 13

[问答] 数据框某列分成2列 [推广有奖]

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

楼主
dxystata 发表于 2016-11-18 20:52:35 |AI写论文
20论坛币
  1. x1<-c('Platform: GPL11154 6 Samples',
  2.       'Platform: GPL10558 28 Samples',
  3.       'Platforms: GPL18149 GPL17077 24 Samples',
  4.       'Platforms: GPL13112 GPL17021 GPL16791 31 Samples',
  5.       'Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 70 Samples',
  6.       'Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 110 Samples')
  7. x2<-c('Series Accession: GSE8894 ID: 200008894',
  8.       'Series Accession: GSE67471 ID: 200067471',NA,NA,NA,NA)
  9. aaa<-data.frame(x1,x2)
复制代码
把aaa$x1这一列分成两列
V1                                  V2
Platform: GPL11154                       6 Samples
Platform: GPL10558                       28 Samples
Platforms: GPL18149 GPL17077                24 Samples
Platforms: GPL13112 GPL17021 GPL16791          31 Samples
Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570    70 Samples
Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570    110 Samples

把aaa$x2这一列分成两列
V3                                 V4
Series Accession: GSE8894                  ID: 200008894
Series Accession: GSE67471               ID: 200067471
谢谢!

关键词:数据框 Series

沙发
ly_0 发表于 2016-11-18 22:18:16
  1. x1
  2. [1] "Platform: GPL11154 6 Samples"                              
  3. [2] "Platform: GPL10558 28 Samples"                             
  4. [3] "Platforms: GPL18149 GPL17077 24 Samples"                  
  5. [4] "Platforms: GPL13112 GPL17021 GPL16791 31 Samples"         
  6. [5] "Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 70 Samples"
  7. [6] "Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 110 Samples"
  8. pat <- "\\s\\d+\\s.+"
  9. addr <- regexpr(pat,x1)
  10. fir <- substr(x1,1,addr)
  11. sec <- substring(x1,addr)
  12. fir
  13. [1] "Platform: GPL11154 "                             "Platform: GPL10558 "                           
  14. [3] "Platforms: GPL18149 GPL17077 "                   "Platforms: GPL13112 GPL17021 GPL16791 "         
  15. [5] "Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 " "Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570 "
  16. sec
  17. [1] " 6 Samples"   " 28 Samples"  " 24 Samples"  " 31 Samples"  " 70 Samples"  " 110 Samples"
  18. result <- data.frame(fir,sec)
  19. result
  20.                                               fir          sec
  21. 1                             Platform: GPL11154     6 Samples
  22. 2                             Platform: GPL10558    28 Samples
  23. 3                   Platforms: GPL18149 GPL17077    24 Samples
  24. 4          Platforms: GPL13112 GPL17021 GPL16791    31 Samples
  25. 5 Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570    70 Samples
  26. 6 Dataset: GDS4794 Platforms: GPL96 GPL97 GPL570   110 Samples
复制代码

藤椅
johnmy 发表于 2016-11-18 22:19:28
我给你分离了第一列,第二列同理,把"GPL[:digit:]+" 换为 "GSE[:digit:]+"
然后你再组合吧,我没有找到在固定位置分割字符串的函数,有的话更方便。

  1. library(stringr)
  2. library(tidyverse) #a bit big, but there many useful functions
  3. p<-str_locate_all(aaa$x1,"GPL[:digit:]+")%>%
  4.   #replace "GPL[:digit:]+" with "GSE[:digit:]+" for the column 2
  5. lapply(max)%>%unlist()
  6. v1<-str_sub(aaa$x1,1,p)%>%str_trim%>%as_tibble()
  7. v2<-str_sub(aaa$x1,p+2,str_count(aaa$x1))%>%str_trim%>%as_tibble()
  8. v<-cbind(v1,v2)
复制代码

板凳
johnmy 发表于 2016-11-18 22:29:55
ly_0 发表于 2016-11-18 22:18
我搞复杂了,查找倒数第二个空格就可以。

报纸
ly_0 发表于 2016-11-18 22:42:00
johnmy 发表于 2016-11-18 22:29
我搞复杂了,查找倒数第二个空格就可以。
嗯,我不知道怎样去找具体哪个空格,就只好把后边的都匹配出来了。。。

地板
johnmy 发表于 2016-11-18 22:55:33
.+就是匹配后面的内容的意思吗

7
ly_0 发表于 2016-11-18 23:12:52
johnmy 发表于 2016-11-18 22:55
.+就是匹配后面的内容的意思吗
嗯,"."表示任意字符,"+"表示1个或多个,连在一起就是一个或多个字符

8
johnmy 发表于 2016-11-19 00:43:29
ly_0 发表于 2016-11-18 23:12
嗯,"."表示任意字符,"+"表示1个或多个,连在一起就是一个或多个字符
了解了,谢谢。
正则表达式真是太灵活了。

9
jgchen1966 发表于 2016-11-19 01:44:30
用 tidyr::separate 函数,一行程序,只是 变量 sep= 设置要费点力,慢慢调试一下。。
已有 1 人评分经验 论坛币 收起 理由
李会超 + 80 + 20 热心帮助其他会员

总评分: 经验 + 80  论坛币 + 20   查看全部评分

10
johnmy 发表于 2016-11-19 02:50:09
jgchen1966 发表于 2016-11-19 01:44
用 tidyr::separate 函数,一行程序,只是 变量 sep= 设置要费点力,慢慢调试一下。。
确实可以这样,但是分隔符是空格,而前面后面都有空格,不好定位。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 07:46