Data types are not recognized when importing the CSV file

r
csv

#1

Im trying to import the data from CSV file in R. Actually, I need the data in this format

str(train)
‘data.frame’: 891 obs. of 12 variables:
PassengerId: int 1 2 3 4 5 6 7 8 9 10 ... Survived : int 0 1 1 1 0 0 0 0 1 1 …
Pclass : int 3 1 3 1 3 3 1 3 3 2 ... Name : Factor w/ 891 levels “Abbing, Mr. Anthony”,…: 109 191 358 277 16 559 520 629 416 581 …
Sex : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ... Age : num 22 38 26 35 35 NA 54 2 27 14 …
SibSp : int 1 1 0 1 0 0 0 3 0 1 ... Parch : int 0 0 0 0 0 0 0 1 2 0 …
Ticket : Factor w/ 681 levels "110152","110413",..: 525 596 662 50 473 276 86 396 345 133 ... Fare : num 7.25 71.28 7.92 53.1 8.05 …
Cabin : Factor w/ 148 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ... Embarked : Factor w/ 4 levels “”,“C”,“Q”,“S”: 4 2 4 4 4 3 4 4 4 2 …

But it is displaying in this format… I have tried it converting the data type.Also, by adding the colClasses argument in the read_csv command, still there is no use… below is the format it is displaying.


#2

Please paste the code and the output for these three operations here-

  1. reading the csv file
  2. str(dataframe name)
  3. converting “Embarked” variable to factor class
  4. str(dataframe name).

#3

train<-as.data.frame(“train_”)

typeof(train)
[1] “list”

str(train) – output which i got is the picture i pasted in this post.


#4

which library are you using to read the file?

using base R

read.csv()

function should give you the required format, wherein each character column is coded as factor by default.

Below a simulation for the same:


#5

Hi Joshij,

Sorry for late and thankyou for responding … I had got the result in the prescribed format when I have used the parameter stringsAsFactor= TRUE … I’m still wondering, how did you get the result in the format ?? did you use the stringsAsFactor parameter ? and I’m using the base R lib … read.csv()


#6

Hello Manoj,

I didn’t passed any argument. The function by default treats text columns as factor.

The option stringsAsFactor can be set to “False” if you don’t want the text columns as factor. They will be treated as character.


#7

Ok Thank you :slight_smile: