简体   繁体   English

R:在第一个空格之后提取

[英]R: Extracting After First Space

I am working with the R programming language.我正在使用 R 编程语言。 I found this question over here that extracts everything from the RIGHT of the first space:我在这里发现了这个问题,它从第一个空格的右侧提取了所有内容:

#https://stackoverflow.com/questions/15895050/using-gsub-to-extract-character-string-before-white-space-in-r

dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM")

gsub( " .*$", "", dob )
# [1] "9/9/43"   "9/17/88"  "11/21/48"

Is it possible to adapt this code to extract after the first space?是否可以调整此代码以在第一个空格之后提取?

# option 1

12:00 AM/PM, 12:00 AM/PM, 12:00 AM/PM

# option 2 : part 1

 12:00, 12:00 ,  12:00 

# option 2: part 2

AM/PM, AM/PM, AM/PM

# then, concatenate option 2 : part 1 and option 2 : part 2

I thought that maybe switching the syntax of the "gsub" command might accomplish this:我认为也许切换“gsub”命令的语法可能会实现这一点:

 gsub( "$*. ", "", dob )
 gsub( "*$. ", "", dob )

But I don't think I am doing this correctly.但我不认为我这样做是正确的。

Can someone please show me how to do this (option 1 and [option 2 part 1, option 2 part 2])?有人可以告诉我如何做到这一点(选项 1 和 [选项 2 第 1 部分,选项 2 第 2 部分])?

Thanks!谢谢!

Note: Normally, I do this with "Text to Columns" in Microsoft Excel - but I would like to learn how to do this in R!注意:通常,我使用 Microsoft Excel 中的“文本到列”来执行此操作 - 但我想学习如何在 R 中执行此操作!

Do you mean the following?你的意思是以下吗?

dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM", "red1 23 g")

gsub("^\\S+ ", "", dob)

#> [1] "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM" "23 g"

Option 1: Remove the first space and everything before it?选项 1:删除第一个空格和它之前的所有内容?

sub(".*? ", "", dob)
# "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM"

Option 2: Remove the last space and everything before it?选项 2:删除最后一个空格和它之前的所有内容?

sub(".* ", "", dob)
# [1] "AM/PM" "AM/PM" "AM/PM"

Option 3: Remove the first/last space and everything before/after it?选项 3:删除第一个/最后一个空格以及它之前/之后的所有内容?

gsub(" [^ ]+$|^.*? ", "", dob)
# [1] "12:00" "12:00" "12:00"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM