0
df <- data.frame( 
  no = c(1:10),
  name = c("Py", "C", "Mike","Lin", "Terry", "Amada", "Jack.P", "Robber" , "Mary", "Kate"), 
  score = c(1, 2, NA, 4, 5, 6, 7, 10, 5, 7),
  team = c(1, 3, 2, 5, 4, 2, 3, 5, 4, 1)
)

I would like to output 5 txt file by df$team, team = 1 into 1.txt and team = 2 into 2.txt ... and so on, I have not succeeded by my code. Thank you.

n = unique(df[[4]])

for (i in n) {

  data <- df 

  name <- glue("df$team.txt", name = name)
  write.table(data, file = path, sep = ",")
}
Ollie
  • 137
  • 5
  • This is a fairly common question. The linked question and answer show a couple of solutions; there are others. – neilfws Sep 07 '22 at 01:01
  • Sorry about I'm not search right key word, I will improve this problem , sometime always can't get useful wat to search. Thank you. – Ollie Sep 07 '22 at 01:18

1 Answers1

1

Here is an option to achieve this:

library(dplyr)
for (i in unique(df$team)) {
  data <- df %>% filter(team == i)
  write.table(data, file = paste0("df$team",i,".txt"), sep = ",")
}
Bushidov
  • 713
  • 4
  • 16