0

I have a table that starts out looking like this:

test <- data.frame("Number" = c("123", "123", "123", "12", "12", "13"), "Value" = c(45, 46, 47, 33, 20, 45))

I want to create a seperate file for each unique "Number". So there would be 3 output files and I would like them to be called "123.txt" "12.txt" "13.txt". The 3 new tables would look like this:

test_123 <- data.frame("Number" = c("123", "123", "123"), "Value" = c(45, 46, 47))
test_12 <- data.frame("Number" = c("12", "12"), "Value" = c(33, 20))
test_13 <- data.frame("Number" = c("13"), "Value" = c(45))

I have no idea where to even begin with this, so your help is greatly appreciated :)!

Sarah
  • 411
  • 4
  • 14

1 Answers1

0

This can help you:

#List
List <- split(test,test$Number)
#Names
namevec <- paste0(names(List),'.txt')
#Export
mapply(FUN = write.table,List,file=namevec)
Duck
  • 39,058
  • 13
  • 42
  • 84