Title: Syntax for mapply vs for loop
Tags: r;loops;mapply
Question: My goal is to take a data set, 1. subset it and 2. print to .csv files
Example data
library(tidyverse)
mydata <- iris
Method to subset data:
z <- (split(mydata, (as.numeric(rownames(mydata))-1) %/% 50))
str(z)
I'm trying to use mapply and I can't get the syntax right.
mapply(write_csv(z, paste0(z,"file.csv"), col_names = FALSE)
If I were to code it with a for loop here's how I'd do it:
for(i in names(z)){
write_csv(z[[i]], paste0(i,"file.csv"), col_names = FALSE)
How would I go about using mapply?
Comment: you want sapply or lapply (same result for your purpose)

Here is the accepted answer: Wth sapply this should work:
sapply(names(z),function(x){write_csv(z[[x]],paste0(x,"file.csv"), col_names = FALSE))
with mapply it would work this way
mapply(function(x,y){write_csv(x,paste0(y,"file.csv"), col_names = FALSE)},z,names(z))
Comment for this answer: I couldn't get `sapply` to work but `mapply` worked perfectly.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:06/14/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><loops><mapply>', 'Title': 'Syntax for mapply vs for loop', 'ViewCount': 103, 'id': 44551754, 'user_AboutMe': '', 'user_AccountId': 8681400.0, 'user_DisplayName': 'Ryan John', 'user_Reputation': 1372.0, 'user_Views': 69.0, 'user_id': 6497137.0}}