Maho Takahashi

Linguistics PhD Student at UC San Diego

research

CV

code

mtakahas[at]ucsd[dot]edu

How to remove unwanted “X” column

When importing a csv file, it often comes with an extra index column “X”.

data = read.csv("./sample.csv")

print(data)
##   X   Column1    Column2 Column3
## 1 1 0.2875775 -1.6895557       8
## 2 2 0.7883051  1.2394959       3
## 3 3 0.4089769 -0.1089660       2
## 4 4 0.8830174 -0.1172420       4
## 5 5 0.9404673  0.1830826       9

To get rid of this unwanted column, you can do one of the following:

#set `row.names` parameter as 1
data1 = read.csv("./sample.csv", row.names = 1)

data1
##     Column1    Column2 Column3
## 1 0.2875775 -1.6895557       8
## 2 0.7883051  1.2394959       3
## 3 0.4089769 -0.1089660       2
## 4 0.8830174 -0.1172420       4
## 5 0.9404673  0.1830826       9
#drop the first column after importing
data2 = data[,-1]

print(data2)
##     Column1    Column2 Column3
## 1 0.2875775 -1.6895557       8
## 2 0.7883051  1.2394959       3
## 3 0.4089769 -0.1089660       2
## 4 0.8830174 -0.1172420       4
## 5 0.9404673  0.1830826       9

Or if you want to prevent the unwanted column when you write a csv file, you can do so by setting the row.names parameter as false.

data_new = data.frame(
  Column1 = runif(5), 
  Column2 = rnorm(5),   
  Column3 = rpois(5, lambda = 5) 
)

write.csv(data_new, "sample_new.csv", row.names = FALSE)
data_new = read.csv("./sample_new.csv")

print(data_new)
##     Column1     Column2 Column3
## 1 0.4623219 -0.04585982       7
## 2 0.6027927 -0.12403548       5
## 3 0.7967110 -0.16816196       5
## 4 0.9127832  1.18376612       6
## 5 0.4468515 -1.69844565       3