The goal of this tutorial is to save a dataframe into a csv without the column names. By definition a dataframe contains column names as every column is a variable. However for certain analysis this is not true and we may want to modify a csv file using a dataframe without inheriting the name of the columns.
# First of all we load the data
# For this tutorial we are going to use the iris plant dataset
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# In order to write a csv without saving the colnames we need to use the write.table function
write.table(iris, file = "iris.csv", col.names = FALSE, row.names = FALSE, sep = ",")
# Now we load the data
head(read.table("iris.csv", header = FALSE, sep = ","))
## V1 V2 V3 V4 V5
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
# We see that column names have not been kept. If you open the csv with a notebook you will see that only the content of the columns has been stored
#5.1,3.5,1.4,0.2,"setosa"
#4.9,3,1.4,0.2,"setosa"
#4.7,3.2,1.3,0.2,"setosa"
#4.6,3.1,1.5,0.2,"setosa"
#etc
In this tutorial we have learnt how to save a dataframe without keeping the column names. In the case of transactional data this process may be mandatory in order to modify the file without introducing headers or column names.