有一個將英文數字(一,二)轉換為數字(1, 2)的函式:
library(remotes)
remotes::install_github("fsingletonthorn/words_to_numbers")
library(wordstonumbers)
輸入和輸出是:
input: words_to_numbers("one and threefold and five tennis")
output: "1 and threefold and 5 tennis"
它一次運行良好,我的問題是如何對一個資料框中有超過 1000 個觀察值的列執行相同的操作。
對于名為“data”的資料框,需要轉換的列是 data$texts,我試過:
data <- within(data, {
texts_new <- words_to_numbers(texts)
})
得到:
The argument which was passed to words_to_numbers is not a length 1 character element, only the first element has been used here. Consider using the apply or purrr::map functions to assess multiple elements at once.
uj5u.com熱心網友回復:
如何將它與sapply()or一起使用的示例map_chr():
library(wordstonumbers)
df <- tibble::tibble(words = c("one and threefold and five tennis",
"ninety-nine red balloons",
"The answer is forty-two"))
# sapply()
df$as_numbers_lapply <- sapply(df$words, words_to_numbers)
# or map_chr()
df$as_numbers_map <- purrr::map_chr(df$words, words_to_numbers)
df
#> # A tibble: 3 × 3
#> words as_numbers_lapply as_numbers_map
#> <chr> <chr> <chr>
#> 1 one and threefold and five tennis 1 and threefold and 5 tennis 1 and threefol…
#> 2 ninety-nine red balloons 99 red balloons 99 red balloons
#> 3 The answer is forty-two The answer is 42 The answer is …
創建于 2022-11-15,使用reprex v2.0.2
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/534259.html
標籤:r功能循环文本数据清理
下一篇:回圈遍歷R中變數名稱的向量
