[英]is there a way to convert a string with numbers to a list of numeric values in R?
假設我有以下
l="1,2,3,4,5"
如何獲取數值序列
1 2 3 4 5
我已經看過這個例子https://statisticsglobe.com/convert-character-to-numeric-in-r/但它與上面的問題不太匹配。
這是這樣做的一種方法:
library(stringr)
l="1,2,3,4,5"
as.numeric(str_split(l, ',', simplify = TRUE))
1) scan 會將這樣的字符串轉換為數字向量。 如果您希望它報告結果的長度,請忽略 quiet 參數。 不使用任何包。
x <- "1,2,3,4,5"
scan(text = x, sep = ",", quiet = TRUE)
## [1] 1 2 3 4 5
2)如果你所擁有的實際上是一個逗號分隔的字符串向量。 二十。 並且需要一個數字向量列表,然后覆蓋它們。
xx <- c(x, x)
lapply(xx, function(x) scan(text = x, sep = ",", quiet = TRUE))
## [[1]]
## [1] 1 2 3 4 5
##
## [[2]]
## [1] 1 2 3 4 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.