Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从R中的strsplit()中获取最后的空元素?_R_String_Csv_Strsplit - Fatal编程技术网

如何从R中的strsplit()中获取最后的空元素?

如何从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 它们都返回整数向量

我需要处理一些主要是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

它们都返回整数向量。您可以将
包装为.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"