a********a 发帖数: 346 | 1 x=function(n){
sim=rnorm(n)
return(sim)
}
x(n=10)
> x(n=10)
[1] 0.3466314 0.7477493 1.1274950 0.3848275 0.9549582 1.1843009
[7] 0.3804086 -1.4802425 1.4219162 1.5410326
> sim
Error: object 'sim' not found
I generate dataset 'sim' from a R function. As you can see from the result,
I can see the data I generated, but why the sim could not be found? As I
can not get sim, so I can not save the data as a text file. Do you know how
to save a dataset generated within a function?
Thanks |
l*********s 发帖数: 5409 | 2 sim is a local object, which is deleted upon finishing of your function call |
a********a 发帖数: 346 | 3 I think you are right. Do you know how can I get the dataset?
Thanks |
b**********i 发帖数: 1059 | 4 This is the most basic question I've ever seen. You use sim<-x(10)
【在 a********a 的大作中提到】 : I think you are right. Do you know how can I get the dataset? : Thanks
|
D******n 发帖数: 2836 | 5 This is one of the dumb things about R, it doesn't have pass by reference
mechanism..... |
a********a 发帖数: 346 | 6 Thank you guys. I figured out it will also work if I write table inside the
function.
x=function(n){
sim=rnorm(n)
return(sim)
write(sim,"path")
}
x(n=10) |