使用R中的for循环函数将输出写入csv
我有两个表,我想把它们输出到几个文件中。该表如下所示:使用R中的for循环函数将输出写入csv,r,csv,for-loop,dataframe,datatable,R,Csv,For Loop,Dataframe,Datatable,我有两个表,我想把它们输出到几个文件中。该表如下所示: df1 --- Colors Red Blue 及 我想根据颜色将此数据写入csv文件。这意味着文件1将包含 Colors Name Rate Red A 3 Red B 5 Red C 6 Colors Name Rate
df1 ---
Colors
Red
Blue
及
我想根据颜色将此数据写入csv文件。这意味着文件1将包含
Colors Name Rate
Red A 3
Red B 5
Red C 6
Colors Name Rate
Blue D 2
Blue E 8
文件二将包含
Colors Name Rate
Red A 3
Red B 5
Red C 6
Colors Name Rate
Blue D 2
Blue E 8
我试过了
output_file <- c()
for (i in 1:nrow(df1)){
if (df1$Colors[i] == df2$Colors[i]){
output_file[i] <- df2[i]
write.csv(output_file, paste("Color_", i, ".csv"), row.names = F)
}
}
但是有语法错误,我找不到!语法错误在哪里?您只需要df2
您可以通过以下方式使用group_。。。并且做
非常感谢你的代码!真的很感激!
mtcars %>%
split(.$cyl) %>%
map(., ~write_tsv(.x, paste0(path, unique(.$cyl))))