从复制的列表中提取一些字符串到字符变量中。

huangapple go评论61阅读模式
英文:

Extract some strings from a copyed list outside r to a character variable

问题

I am looking for a way to convert specific words from a list outside of R to a character vector of strings.
我正在寻找一种将R之外的特定单词从列表转换为字符串字符向量的方法。

I need this as I am doing some regex work and the words will be used as filter terms in a data frame.
我需要这个,因为我正在进行一些正则表达式工作,这些单词将用作数据框中的过滤条件。

For instance, let's say I have a series of unquoted words (e.g. If I were to type in the source screen of r-studio):
例如,假设我有一系列未引用的单词(例如,如果我在r-studio的源屏幕上输入):

audi
chevrolet
honda
ford

I want the above to be turned into:
我想将上述内容转化为:

strings = c("audi","chevrolet","honda","ford")

As then, I can use it in the following filter:
因为然后,我可以在以下过滤器中使用它:

mpg %>% filter(manufacturer %in% strings)

The above use is just an example. I'm really looking for a way to turn unquoted text, that has been either entered manually into R or copy-pasted into R, into a comma-separated character vector that can be used for various things. Also, what is the unquoted text that is not a comment in R?
上述用法只是一个示例。我真正想找的是一种将未引用的文本转换为逗号分隔的字符向量的方法,无论是手动输入到R中还是从R中复制粘贴,可以用于各种用途。另外,在R中未引用的文本是什么?

英文:

I am looking for a way to convert specific words from a list outside of R to a character vector of strings.
I need this as I am doing some regex work and the words will be used as filter terms in a data frame.

For instance, let's say I have a series of unquoted words (e.g. If I were to type in the source screen of r-studio):

audi
chevrolet
honda
ford

I want the above to be turned into:

strings = c("audi","chevrolet","honda","ford")

As then, I can use it in the following filter:

mpg %>% filter(manufacturer %in% strings)

The above use is just an example. I'm really looking for a way to turn unquoted text, that has been either entered manually into R or copy-pasted into R, into a comma-separated character vector that can be used for various things. Also, what is the unquoted text that is not a comment in R?

答案1

得分: 1

Using readLines:

strings <- readLines(con = textConnection("audi
chevrolet
honda
ford"))

strings
#[1] "audi"      "chevrolet" "honda"     "ford"

We can also use RStudio Multiple Cursors, and see related post: https://stackoverflow.com/questions/71472412/fastest-way-to-edit-multiple-lines-of-code-at-the-same-time

英文:

Using readLines:

strings  &lt;- readLines(con = textConnection(&quot;audi
chevrolet
honda
ford&quot;))

strings
#[1] &quot;audi&quot;      &quot;chevrolet&quot; &quot;honda&quot;     &quot;ford&quot;

We can also use RStudio Multiple Cursors, and see related post: https://stackoverflow.com/questions/71472412/fastest-way-to-edit-multiple-lines-of-code-at-the-same-time

答案2

得分: 0

"str_split"来自"stringr"也是一个选项。

你可以将你的字符串复制到一个r字符变量中,你可以看到"\n"是你的分割模式。

library(stringr)
st="audi
chevrolet
honda
ford"
cat(st)
#> audi
#> chevrolet
#> honda
#> ford
print(st)
#> [1] "audi\nchevrolet\nhonda\nford"
st2=str_split(st,pattern = "\n")[[1]]
print(st2)
#> [1] "audi"      "chevrolet" "honda"     "ford"

创建于2023-04-19,使用reprex v2.0.2

英文:

str_split from stringer is an option too.

You can copy past your string to an r character variable, you can see that \n the Line Feed is you splitting pattern

library(stringr)
st=&quot;audi
chevrolet
honda
ford&quot;
cat(st)
#&gt; audi
#&gt; chevrolet
#&gt; honda
#&gt; ford
print(st)
#&gt; [1] &quot;audi\nchevrolet\nhonda\nford&quot;
st2=str_split(st,pattern = &quot;\\n&quot;)[[1]]
print(st2)
#&gt; [1] &quot;audi&quot;      &quot;chevrolet&quot; &quot;honda&quot;     &quot;ford&quot;

<sup>Created on 2023-04-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月19日 17:41:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定