How To import file in R using Reac.table function


#1

Hi ,
I was trying o import file using read.table function but there was some error and it was getting imported. Kindly inform me was there any error in the syntax.
I am attaching the R screen


#2

@sanchi_singh1 : R is unable to read single backslash (i.e. \ ) . Instead of “” u have to use “/” or "\ ".
Below is the example :

data1 = read.table(file = “D:/PP/ test/xyz.csv”, header = T, sep = “,”)


#3

@sanchi_singh1 - Why dont u try

data1 = read.csv(“D:/PP/Importing File in R/LungCapdatacsv.csv”, header = T, sep =",", stringsAsFactors=F) # This is the easiest method to import Csv in R

and if your data size is big use
library(data.table)
data1 = fread(“D:/PP/Importing File in R/LungCapdatacsv.csv”, header = T, sep =",", stringsAsFactors=F)


#4

Thanks a ton Hunaid. I tried the above given function read.csv and file was imported successfully. Please tell me why we use stringsAsFactors=F.
Thanks again.
Regards
Sanchi


#5

Thanku saurabh…:slight_smile:


#6

@sanchi_singh1

By default read csv converts Character variables to Factors. So it does not allow character to be converted to Factors. Hope this helps!!! :slight_smile:


#7

So that means if there is a variable say Gender which contains character of strings Female and Male so R will store them as 0 and 1 is it??
I was trying to use levels function but it is returning null. Please help me out with this. Thanks in advance…:slight_smile:


#8

@sanchi_singh1
I Hope this will help


#9

Thank you Hunaid. Understood Thanks a Ton…:slight_smile:


#10

@sanchi_singh1 Your Welcome :+1: