簡體   English   中英

是否可以在不使用 strsplit 且僅使用基本 R 函數的情況下訪問 R 中字符串中的字符?

[英]Is it possible to access characters in a string in R without using strsplit and only basic R functions?

假設我在 R 中有字符向量:

x <- "hello world"

我想單獨訪問每個元素。 我知道strsplit中的 strsplit 可以做到這一點,但想知道是否有一種方法可以在不依賴這樣的 function 的情況下訪問元素?

如果您正在尋找替代方案,則可以使用正則表達式。 R 支持的強大工具。

library(tidyverse)
str_extract(x, "\\w+") #you ask to extract each word separated by a space. It includes words or numbers.
#the first \ is to indicate you are using regex.

希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM