Skip to content
Related Articles

Related Articles

Improve Article

How to write to CSV in R without index ?

  • Last Updated : 07 Apr, 2021

We know that when we write some data from DataFrame to CSV file then a column is automatically created for indexing. We can remove it by some modifications. So, in this article, we are going to see how to write CSV in R without index.

To write to csv file write.csv() is used.

Syntax:

write.csv(data,path)

Lets first see how indices appear when data is written to CSV.



Example:

R




Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
  
Population_1_july_2018 <- c("1,427,647,786", "1,352,642,280"
                            "327,096,265", "267,670,543", "212,228,286")
  
Population_1_july_2019 <- c("1,433,783,686", "1,366,417,754"
                            "329,064,917", "270,625,568", "216,565,318")
  
change_in_percents <- c("+0.43%", "+1.02%", "+0.60%", "+1.10%", "+2.04%")
  
  
data <- data.frame(Country, Population_1_july_2018, Population_1_july_2019, change_in_percents)
print(data)
  
write.csv(data,"C:\\Users\\...YOUR PATH...\\population.csv")
print ('CSV file written Successfully :)')

Output:

CSV file with extra index column

CSV file with extra index column

Now let us see how these indices can be removed, for that simply set row.names parameter to False while writing data to a csv file using write.csv() function. By Default, it will be TRUE and create one extra column to CSV file as index column.

Example:

R




Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
  
Population_1_july_2018 <- c("1,427,647,786", "1,352,642,280"
                            "327,096,265", "267,670,543", "212,228,286")
  
Population_1_july_2019 <- c("1,433,783,686", "1,366,417,754"
                            "329,064,917", "270,625,568", "216,565,318")
  
change_in_percents <- c("+0.43%", "+1.02%", "+0.60%", "+1.10%", "+2.04%")
  
  
data <- data.frame(Country, Population_1_july_2018, Population_1_july_2019, change_in_percents)
  
write.csv(data,"C:\\Users\\..YOUR PATH...\\population.csv", row.names = FALSE)
print ('CSV file written Successfully :)')

Output:

CSV file without extra index column

CSV file without extra index column




My Personal Notes arrow_drop_up
Recommended Articles
Page :