How to make a local data frame display in global environment?

I am going to create multiple data frames by a R function, followed by data frame join (combine all data frames), but, data frame created inside the function doesn't display in the global environment, how can use the local data frame in global environment? Thank you.
for example:
newdata<- function(i, a, b) {
mydata_i<- data.frame(x=a, y=b)
return(mydata_i)
}

newdata(1, 2, 3)
newdata(2, 4, 6)

how can I combine two data frames outside function?
last<- rbind(mydata_1, mydata_2)
Here is a simple example, real data is more complicated. Thank you.

You need to assign the result of your function, otherwise it is not "saved" as object in your workspace. With your code:

newdata<- function(i, a, b) {
    mydata_i<- data.frame(x=a, y=b)
    return(mydata_i)
}

# not saved as object, just printed
newdata(1, 2, 3)
newdata(2, 4, 6)

# assign it
mydata_1 <- newdata(1, 2, 3)
mydata_2 <- newdata(2, 4, 6)

# and use it
last<- rbind(mydata_1, mydata_2)

# or use if directly
last2 <- rbind(newdata(1, 2, 3), newdata(2, 4, 6))

Hope it helps.

2 Likes

Thank you for your help! It works.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.