string 用 R 获取逗号前的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19320966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:07:00  来源:igfitidea点击:

Get the strings before the comma with R

stringrsplitextract

提问by user2855907

I am a beginner with R. Now, I have a vector in a data.frame like this

我是 R 的初学者。现在,我在 data.frame 中有一个像这样的向量

city
Kirkland,
Bethesda,
Wellington,
La Jolla,
Berkeley,
Costa, Evie KW172NJ
Miami,
Plano,
Sacramento,
Middletown,
Webster,
Houston,
Denver,
Kirkland,
Pinecrest,
Tarzana,
Boulder,
Westfield,
Fair Haven,
Royal Palm Beach, Fl
Westport,
Encino,
Oak Ridge,

I want to clean it. What I want is all the city names before the comma. How can I get the result in R? Thanks!

我想清理它。我想要的是逗号前的所有城市名称。我怎样才能在R中得到结果?谢谢!

回答by juba

You can use gsubwith a bit of regexp :

您可以使用gsub一些正则表达式:

cities <- gsub("^(.*?),.*", "\1", df$city)

This one works, too :

这个也有效:

cities <- gsub(",.*$", "", df$city)

回答by Jilber Urbina

Just for fun, you can use strsplit

只是为了好玩,你可以使用 strsplit

> x <- c("London, UK", "Paris, France", "New York, USA")
> sapply(strsplit(x, ","), "[", 1)
[1] "London"   "Paris"    "New York"

回答by James

You could use regexprto find the position of the first comma in each element and use substrto snip them at this:

您可以使用regexpr来查找每个元素中第一个逗号的位置,并substr在此处将它们剪断:

x <- c("London, UK", "Paris, France", "New York, USA")

substr(x,1,regexpr(",",x)-1)
[1] "London"   "Paris"    "New York"

回答by Tyler Rinker

This works as well:

这也有效:

x <- c("London, UK", "Paris, France", "New York, USA")

library(qdap)
beg2char(x, ",")

## > beg2char(x, ",")
## [1] "London"   "Paris"    "New York"

回答by Jeereddy

If the this was a column in a dataframe, we can use tidyverse.

如果这是数据框中的一列,我们可以使用 tidyverse。

library(dplyr)
x <- c("London, UK", "Paris, France", "New York, USA")
x <- as.data.frame(x)
x %>% separate(x, c("A","B"), sep = ',')
        A       B
1   London      UK
2    Paris  France
3 New York     USA