Issue while importing file in R

r

#1

Hi All,
I am trying to import AirPassengers.csv (3.2 KB) into R using read.table function.

inputFile <- read.table(file.choose() , header = T , sep = “,”)

This file actually has 2 columns “Time” and “Airpassengers” along with row label ( which is the first column in input file)
Know , when i am importing it , row label is getting imported as one of the variable , increasing the variable count to 3 , but the expected result is that “inputFile” should have only 2 variables.
Please , let me know , how to avoid that.
Thanks.


#2

Hi @Rishabh0709,

You are getting 3 columns because the data set has 3 columns in it. There is nothing wrong with that. You can simply delete the first column if you don’t want it. Use the following code to do so.

inputFile<-inputFile[,-c(1)]

Regards,


#3

Thanks for your response @shashwat.2014.
At first , even i thought that , i can not do anything about it , but when i looked at help for “read.table” ,
i found that , “row.names” might solve my problem
Following is the content , for row.names from read.table help page

row.names

a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.

If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.

But its not working.


#4

Hi,
I wasn’t able to download your csv file using the link in your post but is it possible these row numbers exist in your csv file before you even import the file into R?

This would explain this behavior. row.names=F will only disable R generated row names but will have no effect if the row numbers exist in the csv file you are importing since it will be read in as a column in your data set.

Have you inspected your csv file to verify no other columns are inadvertently present?


#5

@Rishabh0709

Hi there,

data<-read.table("AirPassengers.csv",sep=",",header = TRUE,row.names = 1)

This code will read your row names without any problem.

Regards,
Neeraj


#6

Thanks @NSS