Stochastic Nonsense

Put something smart here.

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:

1
2
3
4
5
> 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:

1
2
3
4
5
6
7
8
9
10
11
$ 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

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:

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

checking, we see the csv file looks like:

1
2
3
4
5
6
7
8
9
10
11
12
$ 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
$