|
Hello. I'm a newbie here. In my script (I name it readData.R), I wrote the followings: readData <-function(){ med = read.csv("medicalData.csv");} Then I tested the script by 'Source R Code' then on the command I typed 'readData()' then I typed 'med' to check if the variable contains the medical data but it returned 'Object med is not found'. What did I do wrong? Please help. Cheers,Suhaila [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
Hi Suhaila,
You don't need to make a function: your script should just contain: med <- read.csv("medicalData.csv") If you do want to make a function, then you need to assign the resulting value to something, eg: med <- readData() but there's no reason to do that. Values that are assigned within a function don't persist in the global environment. Sarah On Mon, May 7, 2012 at 1:41 PM, Suhaila Haji Mohd Hussin <[hidden email]> wrote: > > Hello. I'm a newbie here. > In my script (I name it readData.R), I wrote the followings: > readData <-function(){ med = read.csv("medicalData.csv");} > Then I tested the script by 'Source R Code' then on the command I typed 'readData()' then I typed 'med' to check if the variable contains the medical data but it returned 'Object med is not found'. What did I do wrong? Please help. > Cheers,Suhaila -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Suhaila Haji Mohd Hussin
R is a functional language so, by default, assignments (and other
things) within function scope doesn't have global effects. This is generally considered a _very good thing_ in language design. You'd perhaps prefer something like: readData <- function() { read.csv("medialData.csv") } med <- readData() which will have the desired outcome. On Mon, May 7, 2012 at 1:41 PM, Suhaila Haji Mohd Hussin <[hidden email]> wrote: > > Hello. I'm a newbie here. > In my script (I name it readData.R), I wrote the followings: > readData <-function(){ med = read.csv("medicalData.csv");} > Then I tested the script by 'Source R Code' then on the command I typed 'readData()' then I typed 'med' to check if the variable contains the medical data but it returned 'Object med is not found'. What did I do wrong? Please help. > Cheers,Suhaila > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Suhaila Haji Mohd Hussin
On 07-05-2012, at 19:41, Suhaila Haji Mohd Hussin wrote: > > Hello. I'm a newbie here. > In my script (I name it readData.R), I wrote the followings: > readData <-function(){ med = read.csv("medicalData.csv");} > Then I tested the script by 'Source R Code' then on the command I typed 'readData()' then I typed 'med' to check if the variable contains the medical data but it returned 'Object med is not found'. What did I do wrong? Please help. Have a look at section 10.5 "Assignment within functions" of the "An Introduction to R " manual. Berend ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Sarah Goslee
Thank Sarah! > Date: Mon, 7 May 2012 14:06:31 -0400 > Subject: Re: [R] Problem in executing R-script > From: [hidden email] > To: [hidden email] > CC: [hidden email] > > Hi Suhaila, > > You don't need to make a function: your script should just contain: > med <- read.csv("medicalData.csv") > > If you do want to make a function, then you need to assign the > resulting value to something, eg: > > med <- readData() > > but there's no reason to do that. Values that are assigned within a > function don't persist in the global environment. > > Sarah > > On Mon, May 7, 2012 at 1:41 PM, Suhaila Haji Mohd Hussin > <[hidden email]> wrote: > > > > Hello. I'm a newbie here. > > In my script (I name it readData.R), I wrote the followings: > > readData <-function(){ med = read.csv("medicalData.csv");} > > Then I tested the script by 'Source R Code' then on the command I typed 'readData()' then I typed 'med' to check if the variable contains the medical data but it returned 'Object med is not found'. What did I do wrong? Please help. > > Cheers,Suhaila > > -- > Sarah Goslee > http://www.functionaldiversity.org [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
| Powered by Nabble | Edit this page |
