使用gsub提取R中空格前的字符串 [英] Using gsub to extract character string before white space in R
问题描述
我有一个看起来像这样的生日列表:
I have a list of birthdays that look something like this:
dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM")
我只想从这个变量中获取日历日期(即在第一次出现空格后删除所有内容).
I want to just grab the calendar date from this variable (ie drop everything after the first occurrence of white-space).
这是我迄今为止尝试过的:
Here's what I have tried so far:
dob.abridged <- substring(dob,1,8)
dob
[1] "9/9/43 1" "9/17/88 " "11/21/48"
dob.abridged <- gsub(" $","", dob.abridged, perl=T)
> dob.abridged
[1] "9/9/43 1" "9/17/88" "11/21/48"
所以我的代码适用于长度为 6 或 7 的日历日期,但不适用于长度为 8 的日历日期.是否有任何关于与 gsub 一起使用的更有效正则表达式的指针可以处理长度为 6、7 或 8 的日历日期?
So my code works for calendar dates of length 6 or 7, but not length 8. Any pointers on a more effective regex to use with gsub that can handle calendar dates of length 6, 7 or 8?
谢谢.
推荐答案
No need for substring, just use gsub:
gsub( " .*$", "", dob )
# [1] "9/9/43" "9/17/88" "11/21/48"
一个空格 (),然后是任意字符 (
.
) 任意次数 (*
) 直到字符串结束 (<代码>$).请参阅 ?regex 以了解正则表达式.
A space (), then any character (
.
) any number of times (*
) until the end of the string ($
). See ?regex to learn regular expressions.
这篇关于使用gsub提取R中空格前的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!