У меня есть следующее предложение:
It is tricky for authors to figure out which keys will work, and how their values will be specified. Lamentably, it may be necessary to read the source code of an R package like rmarkdown to understand all of the details. However, usually, there is some relief in the fact that there is one example working document to exemplify the settings for a particular back end and authors can adjust and modify those values. The key:value pairs may may a difference at several stages in the compilation process.
Я пытался разместить весь текст в векторе.
То, что я сделал, это поместил все в вектор.
x<-c("It is tricky for authors to figure out which keys will work, and how their values will be specified. Lamentably, it may be necessary to read the source code of an R package like rmarkdown to understand all of the details. However, usually, there is some relief in the fact that there is one example working document to exemplify the settings for a particular back end and authors can adjust and modify those values. The key:value pairs may may a difference at several stages in the compilation process.")
Затем я использовал функцию strsplit для разделения каждого слова следующим образом:
palabras<-strsplit(x, "\\s")
Что я нахожу:
[1] "It" "is" "tricky" "for" "authors" "to" "figure" "out"
[9] "which" "keys" "will" "work," "and" "how" "their" "values"
[17] "will" "be" "specified." "Lamentably," "it" "may" "be" "necessary"
[25] "to" "read" "the" "source" "code" "of" "an" "R"
[33] "package" "like" "rmarkdown" "to" "understand" "all" "of" "the"
[41] "details." "However," "usually," "there" "is" "some" "relief" "in"
[49] "the" "fact" "that" "there" "is" "one" "example" "working"
[57] "document" "to" "exemplify" "the" "settings" "for" "a" "particular"
[65] "back" "end" "and" "authors" "can" "adjust" "and" "modify"
[73] "those" "values." "The" "key:value" "pairs" "may" "may" "a"
[81] "difference" "at" "several" "stages" "in" "the" "compilation" "process."
Но я не знаю, как преобразовать все эти слова в вектор.
То есть иметь вектор из 88 элементов.
Я не знаю, что еще делать ...
Есть ли функция, которая позволяет мне решить эту проблему простым способом?
Действительно strsplit()
возвращает список, но в этом случае с único элемент, который является вектором символов. Если ты хочешь, ты можешь извлекать прямо этот первый элемент:
palabras <- strsplit(x, "\\s")
class(palabras)
[1] "list"
palabras <- palabras[[1]]
class(palabras)
[1] "character"
Или también podr¦-схвати использовать unlist()
:
unlist(palabras)
Â: Почему тебя возвращает список? просто, из-за которых он способен обрабатывать вектор с múltiples элементы, в твоем одиноком случае есть один:
strsplit(c("Hola que tal", "como te va?"), "\\s")
[[1]]
[1] "Hola" "que" "tal"
[[2]]
[1] "como" "te" "va?"
Для этого случая, где есть многообразные элементы источника, extracci¦n посредством [[
deber¦-схвати делать ее для каждого элемента списка, в против игры unlist()
в этом случае возвращает único полный вектор
unlist(strsplit(c("Hola que tal", "como te va?"), "\\s"))
[1] "Hola" "que" "tal" "como" "te" "va?"