英文:
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 <- 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
答案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="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"
<sup>Created on 2023-04-19 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论