read.csv falsly converts string to integer

后端 未结 3 403
盖世英雄少女心
盖世英雄少女心 2021-01-22 23:19

I would like to read a csv file but there are columns that contain strings of digits (string variable). The values in the csv file are quoted (\"\") so easily identifyable as st

相关标签:
3条回答
  • 2021-01-22 23:25

    You can use as.character() on your two columns.

    Example :

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

    So just write :

    datSwm[,4:5] <- as.character(datSwm[,4:5])
    
    0 讨论(0)
  • 2021-01-22 23:34

    You could use the read.csv argument: colClasses

    colClasses describes the content of the columns (see ?read.csv).

    below an example for the first five columns: you need to drop stringAsFactors (it would be overridden by colClasses)

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

    You will need to add more details for the remaining columns.

    0 讨论(0)
  • 2021-01-22 23:40

    Try this :

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

    0 讨论(0)
提交回复
热议问题