0

HI I want to write objects of list into different csv files, how do i do that like I want elements of dataFrame "a" to be written into different csv file, elements of dataFrame "b" to be written into different csv file and so on. any help would be much appreciated this is what I have tried

a = data.frame(1:5)
b = data.frame(c(11,22,33,44,55))
d = data.frame(c("aa","bb","cc","dd","ee"))
e = data.frame(c(TRUE,FALSE,TRUE,TRUE,FALSE))
g = data.frame(c(1,0,1,0,1))
myList <- list(a,b,d,e,g)
myfunction <- function(myList) {
  for(i in 1:myList) {
    write.table(myList[i], file = paste(names(myList), ".csv", sep = "",sep = ",")
  }
 }

1 Answer 1

1

First of all you got several typos in your code. E.g. not closing the bracket after your paste command, etc.

However, the main reason why your code does not work is, because you did not create a named list. You need to name your list elements, if you want to use names on the list. Otherwise names(myList) will return just NULL. Have a look at how myList is defined now.

a = data.frame(1:5)
b = data.frame(c(11,22,33,44,55))
d = data.frame(c("aa","bb","cc","dd","ee"))
e = data.frame(c(TRUE,FALSE,TRUE,TRUE,FALSE))
g = data.frame(c(1,0,1,0,1))
myList <- list(a=a,b=b,d=d,e=e,g=g)

myfunction <- function(myList) {
  for(i in seq(1,length(myList))) {
  write.table(myList[[i]], file = paste(names(myList)[i], ".csv", sep=""),sep = ",")
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

apologies! I being a novice this type of typos has occur,anyways this is what I expected ..Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.