Reputation: 11
I want to write a dataframe to a file.
It works like this:
df <- data.frame(x = xalues, y = yalues)
write.csv(df, 'file.csv')
But it does not work when I put it in a function:
writing<-function(df){
write.csv(df, 'file.csv')
}
df <- data.frame(x = xalues, y = yalues)
writing(df)
This is my whole function:
gccount <- function(bestand){
library(dict)
library(stringr)
bieb<-dict()
for (i in bestand){
for (j in i){
titel<-grepl(">Rosalind",j)
if (titel==TRUE){
bieb[[j]]<-""
bewaar<-j
}
else if (titel==FALSE){
vorige<-bieb[[bewaar]]
if (vorige==""){
bieb[[bewaar]]<-j
}
else if (vorige!=""){
bieb[[bewaar]]<-paste(vorige,j,sep = "")
}
}
}
}
bieb$items()
gccbieb<-dict()
for (naam in bieb$keys()){
waarde<-bieb[[naam]]
g<-str_count(waarde,"G")
c<-str_count(waarde,"C")
gcc<-100/nchar(waarde)*(g+c)
gccbieb[[naam]]<-gcc
}
keys<-gccbieb$keys()
values<-gccbieb$values()
sleutels<-c()
gaten<-c()
for (key in keys){
sleutels<-c(sleutels, key)
}
for (value in values){
gaten<-c(gaten, value)
}
df <- data.frame(x = sleutels, y = gaten)
write.csv(df, 'gc.csv')
}
bestand<-read.csv('Rosalind.csv', header = FALSE, sep = ".")
gccount(bestand)
I don't get a file, but when I split it into two different functions, one to make the dictionary gccbieb
, and one to write a file, I do get a file.
Upvotes: 1
Views: 1387
Reputation: 5958
I think this function is working perfectly well, but the folder it writes the file in is another one that you are expecting.
Try to write the full path of "file.csv" or look into your current working directory which you can identify by getwd()
EDIT: can you try with df <<- data.frame(x = sleutels, y = gaten)
instead of <-
?
By the way, what version of R are you using? I have the error package ‘dict’ is not available (for R version 3.5.1)
, if the solution proposed is not working, try to give us a reproducible example.
Upvotes: 1