如何从R中的strsplit()中获取最后的空元素?
我需要处理一些主要是csv的数据。问题是,如果逗号出现在一行的末尾,R会忽略它(例如,在下面的示例中,逗号出现在3之后)如何从R中的strsplit()中获取最后的空元素?,r,string,csv,strsplit,R,String,Csv,Strsplit,我需要处理一些主要是csv的数据。问题是,如果逗号出现在一行的末尾,R会忽略它(例如,在下面的示例中,逗号出现在3之后) 我希望它被读为[1]“1”“2”“3”NA。我该怎么做?谢谢。这里有一些想法 scan(text="1,2,3,", sep=",", quiet=TRUE) #[1] 1 2 3 NA unlist(read.csv(text="1,2,3,", header=FALSE), use.names=FALSE) #[1] 1 2 3 NA 它们都返回整数向量
我希望它被读为
[1]“1”“2”“3”NA
。我该怎么做?谢谢。这里有一些想法
scan(text="1,2,3,", sep=",", quiet=TRUE)
#[1] 1 2 3 NA
unlist(read.csv(text="1,2,3,", header=FALSE), use.names=FALSE)
#[1] 1 2 3 NA
它们都返回整数向量。您可以将
包装为.character
,以获得问题中显示的确切输出:
as.character(scan(text="1,2,3,", sep=",", quiet=TRUE))
#[1] "1" "2" "3" NA
或者,您可以在scan
中指定what=“character”
,或者在read.csv
中指定稍微不同的输出
scan(text="1,2,3,", sep=",", quiet=TRUE, what="character")
#[1] "1" "2" "3" ""
unlist(read.csv(text="1,2,3,", header=FALSE, colClasses="character"), use.names=FALSE)
#[1] "1" "2" "3" ""
您还可以指定na.strings=”“
以及colClasses=“character”
Hadley的
stringi
(以及之前的stringr
)库是对基本字符串函数(完全矢量化、一致的函数接口)的巨大改进:
使用
stringi
包:
require(stringi)
> stri_split_fixed("1,2,3,",",")
[[1]]
[1] "1" "2" "3" ""
## you can directly specify if you want to omit this empty elements
> stri_split_fixed("1,2,3,",",",omit_empty = TRUE)
[[1]]
[1] "1" "2" "3"
stringr
很慢,你应该使用stringi
:)@silvaran你完全正确,我写这篇文章后才意识到stringi
。(到底该如何掌握R中最新最棒的软件包?)
unlist(read.csv(text="1,2,3,", header=FALSE, colClasses="character", na.strings=""),
use.names=FALSE)
#[1] "1" "2" "3" NA
require(stringr)
str_split("1,2,3,", ",")
[1] "1" "2" "3" ""
as.integer(unlist(str_split("1,2,3,", ",")))
[1] 1 2 3 NA
require(stringi)
> stri_split_fixed("1,2,3,",",")
[[1]]
[1] "1" "2" "3" ""
## you can directly specify if you want to omit this empty elements
> stri_split_fixed("1,2,3,",",",omit_empty = TRUE)
[[1]]
[1] "1" "2" "3"