Removing Extra Column of Data from CSVs in R — R Tip

When R writes a csv file, you get an extra column of data as such:

> s <- data.frame(x=1:30, y=10*runif(n=30) )
>
> plot(x=s$x, y=s$y )
>
> write.csv(x=s, file='s0.csv' )

When you peek in the csv file, you see this:

blog earl$ head s0.csv
"","x","y"
"1",1,8.29164186026901
"2",2,2.83956938423216
"3",3,7.43510165950283
"4",4,6.38210728997365
"5",5,9.29241271456704
"6",6,6.13102467032149
"7",7,5.03747826907784
"8",8,1.83257902506739
"9",9,9.62789378128946
blog earl$

What is that first column? It’s actually pretty obvious in this example, but if you’ve sorted your data frame a couple times it will appear to be a random sequence of integers. It’s the row names from your data frame, and to suppress it, you use row.names = false:

> write.csv(x=s, file='s1.csv', row.names=F)
>

checking, we see the csf file looks like:

earl$ head s1.csv
"x","y"
1,8.29164186026901
2,2.83956938423216
3,7.43510165950283
4,6.38210728997365
5,9.29241271456704
6,6.13102467032149
7,5.03747826907784
8,1.83257902506739
9,9.62789378128946
blog earl$

This entry was posted in Data Munging, R, R Tip and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>