0

I have a function that transforms a dataframe's structure (making each value for "year_season" factor as its own column) for each value in a list (a list of species names) and reorders the 1st column values (the location or "site") to be in order (low to high). I then tell it to save the dataframe as a matrix and print each matrix as a csv. For some reason though, the function is making a another column out of the rownames and I can't remove it.

data = "dat":

> dat[sample(nrow(dat), 5), ] #Selects 5 random rows of data
       site year_season            common_name num
171930   41    2016_wet     Longsnout Seahorse   0
110012   18    2012_wet                Tomtate   5
2594     24    2005_dry                  Porgy   0
90037    28    2011_dry Florida Grassflat Crab   1
5444     41    2005_dry              Code Goby   2

Transformed data (a matrix like this for each species):

enter image description here

# Summarize total counts by species common_name
spSummary <- dat %>% group_by(common_name) %>%
  dplyr::summarize(total.numbers=sum(num)) %>%
  arrange(-total.numbers)
spSummary

splist<-spSummary$common_name
dataList<-list()
filenameVal<-paste0(1:length(splist), splist, " - my_matrix.csv")

GetMatrix=function(data,spToSum) {
  newdat<-filter(data,common_name %in% spToSum)
  matrixdat<-dcast(newdat, site ~ year_season, value.var="num", fun.aggregate = sum, fill=NA)
  matrixdat$site <- as.character(matrixdat$site)
  matrixdat <- matrixdat[order(nchar(matrixdat$site), matrixdat$site)]
  matrixdat <- as.matrix(matrixdat[,-1]) # This removes "site"
}


for(run in 1:length(splist)) {
  dataList[[run]]<-GetMatrix(dat,splist[run])  
  mapply(write.csv, x=dataList, file=filenameVal[run])
  print(paste(run,splist[run]))
}

This is an example of one of the csv files with this 1st column that I can't get rid of...

enter image description here

Nate
  • 411
  • 2
  • 10

1 Answers1

1

That's the default functionality of write.table and write.csv. Add row.names=FALSE. From ?write.csv:

Usage:

     write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
                 eol = "\n", na = "NA", dec = ".", row.names = TRUE,
                 col.names = TRUE, qmethod = c("escape", "double"),
                 fileEncoding = "")
     
     write.csv(...)
     write.csv2(...)

Arguments:

   ...

row.names: either a logical value indicating whether the row names of
          'x' are to be written along with 'x', or a character vector
          of row names to be written.

Update your for loop as:

for(run in 1:length(splist)) {
  dataList[[run]]<-GetMatrix(dat,splist[run])  
  mapply(write.csv, x=dataList, file=filenameVal[run], row.names=FALSE) # CHANGE
  print(paste(run,splist[run]))
}

Demo with real data:

write.csv(iris[1:3,])
# "","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# "1",5.1,3.5,1.4,0.2,"setosa"
# "2",4.9,3,1.4,0.2,"setosa"
# "3",4.7,3.2,1.3,0.2,"setosa"

write.csv(iris[1:3,], row.names=FALSE)
# "Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# 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"
r2evans
  • 141,215
  • 6
  • 77
  • 149