Вы можете просто вызвать функцию, используя lapply
следующим образом:
lapply(1:2,my.function)
Выход:
[[1]]
a b
1 1 a
2 2 b
[[2]]
a b
1 I a
2 II b
Если вы хотите следовать вашему подходу, то:
my.function <- function(x){
for(i in 1:length(x)){ # This will call for each element in x
if (1 %in% x) {
first.data <- data.frame(a = c(1, 2), b = c("a", "b"))
data = (first.data) # Store intermediate result to data
}
if (2 %in% x){
second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
data=rbind(data,second.data) # Row wise bind the result
}
return(data) # Return the data
}
}
my.function(x = c(1, 2))
Выход:
a b
1 1 a
2 2 b
3 I a
4 II b