read.csv将字符串转换为整数

时间:2017-03-24 11:00:41

标签: r import

我想读取一个csv文件,但是有些列包含数字字符串(字符串变量)。引用csv文件中的值(""),因此很容易识别为字符串变量,但由于某种原因,它们在我的data.frame中最终为整数。

这是csv

的负责人
"task","itemnr","respnr","checked","solution","score","userid","filenr","timestamp","swmClicks","swmRT"
"swm",1,"E1","010010010","000111000",0,"77279","77279","2017-02-14T12:58:56.457+0430",3,13.0379998683929
"swm",10,"E1","011001000","011001000",1,"77279","77279","2017-02-14T13:01:50.717+0430",6,20.4059998989105

问题在于第4和第5列。

这是我使用的代码。有什么问题吗?

datSwm <- read.csv("datSwm.csv", header=T, stringsAsFactors=FALSE, quote='\"')

3 个答案:

答案 0 :(得分:1)

试试这个:

datSwm <- read.csv("datSwm.csv", header=T, stringsAsFactors=FALSE, quote='\"',colClasses=c("character","numeric","character","character","character","numeric","character","character","character","numeric","numeric"))

答案 1 :(得分:1)

您可以使用read.csv参数:colClasses

colClasses描述了列的内容(参见?read.csv)。

下面是前五列的示例: 你需要删除stringAsFactors(它会被colClasses覆盖)

datSwm <- read.csv("datSwm.csv", header=T, quote='\"', 
colClasses = c("factor", "numeric", "character", "character", "character") )

您需要为剩余的列添加更多详细信息。

答案 2 :(得分:0)

您可以在两列上使用as.character()

示例:

vec <- c(1,2,3)
> vec
[1] 1 2 3

vec <- as.character(vec)
> vec
[1] "1" "2" "3"

所以写一下:

datSwm[,4:5] <- as.character(datSwm[,4:5])