Abhinav S Abhinav S - 3 months ago 8
R Question

For a data frame, am trying to list out all columns and unique fields in those columns and write this to a csv file

##Initialise empty dataframe
g <-data.frame(x= character(), y= character(),z=numeric())

## Loop through each columns and list out unique values (with the column name)
for(i in 1:ncol(iris))
{
a<-data.frame(colnames(iris)[i],unique(iris[,i]),i)
g<-rbind(g,a)
setNames(g,c('x','y','z'))
}
## write the output to csv file
write.csv(g,"1.csv")


The output CSV file is something like this
CSV File output

Now the Column headers i want are not proper. I want column headers to be 'X','Y','Z' respectively. Also the first column should not be there.

If you have any other efficient way to do this, let me know.
Thanks!

Answer

This will do the work:

for(i in 1:ncol(iris))
{
a<-data.frame(colnames(iris)[i],unique(iris[,i]),i)
g<-rbind(g,a)
}
g <- setNames(g,c('x','y','z'))   ## note the `g <-`
write.csv(g,"1.csv")

setNames returns a new data frame with names "x", "y" and "z", rather than updating the input data frame g. You need the explicit assignment <- to do the "replacement". You may hide such <- by using either of the two

names(g) <- c('x','y','z')
colnames(g) <- c('x','y','z')

Alternatively, you can use the col.names argument inside write.table:

for(i in 1:ncol(iris))
{
a<-data.frame(colnames(iris)[i],unique(iris[,i]),i)
g<-rbind(g,a)
}
write.table(g, file = "a.csv", col.names=c("x","y","z"), sep =",", row.names=FALSE)

write.csv() does not support col.names, hence we use write.table(..., sep = ","). Trying to use col.names in write.csv will generate a warning.