[R] Reading data/variables
Sarah Goslee
sarah.goslee at gmail.com
Thu Nov 17 12:57:55 CET 2011
Well, if your problem is that a workspace is being loaded automatically
and you don't want that workspace, you have several options:
1. Use a different directory for each project so that the file loaded
by default is the correct one.
2. Don't save your workspace, but regenerate it each time.
3. Use R --vanilla or your OS's equivalent to start R without loading anything
automatically, and use load() and save() to manually manage RData files.
Yes, it's convenient, but if you want to use a non-standard way of working
you need to understand what you're doing.
Sarah
On Thu, Nov 17, 2011 at 3:10 AM, Steven Yen <syen at utk.edu> wrote:
> Thanks Sarah. I have read about the problems with attach(), and I will try
> to avoid it.
> I have now found the line that's causing the problem is:
>
> setwd("z:/homework")
>
> With that line in place, either in a program or in Rprofile.site (?), then
> the moment I run R and simply enter (before reading any data)
> summary(mydata)
> I get sample statistics for a dozen variables!
>
> Do not save the workspace? I thought the option to save/use a binary file is
> meant to be convenient.
>
> I like working in the same working directory, and I like .rdata files. Does
> this sound hopeless? Thanks.
>
> At 09:26 PM 11/15/2011, Sarah Goslee wrote:
>
> Hi,
>
> The obvious answer is don't use attach() and you'll never have
> that problem. And see further comments inline.
>
> On Tue, Nov 15, 2011 at 6:05 PM, Steven Yen <syen at utk.edu> wrote:
>> Can someone help me with this variable/data reading issue?
>> I read a csv file and transform/create an additional variable (called y).
>>
>> The first set of commands below produced different sample statistics
>> for hw11$y and y
>>
>> In the second set of command I renameuse the variable name yy, and
>> sample statistics for $hw11$yy and yy are identical.
>>
>> Using y <- yy fixed it, but I am not sure why I would need to do that.
>>
>> That "y" appeared to have come from a variable called "y" from
>> another data frame (unrelated to the current run).
>>
>> Help!
>>
>> > setwd("z:/homework")
>> > sink ("z:/homework/hw11.our", append=T, split=T)
>> > hw11 <- read.csv("ij10b.csv",header=T)
>> > hw11$y <- hw11$e3
>> > attach(hw11)
>> The following object(s) are masked _by_ '.GlobalEnv':
>> y
>
> Look there. R even *told* you that it was going to use the
> y in the global environment rather than the one you were
> trying to attach.
>
> The other solution: don't save your workspace. Your other
> email on this topic suggested to me that there is a .RData
> file in your preferred working directory that contains an
> object y, and that's what is interfering with what you think
> should happen.
>
> Deleting that file, or using a different directory, or removing
> y before you attach the data frame would all work.
>
> But truly, the best possible strategy is to avoid using attach()
> so you don't have to worry about which object named y is
> really being used because you specify it explicitly.
>
>
>> > (n <- dim(hw11)[1])
>> [1] 13765
>> > summary(hw11$y)
>> Min. 1st Qu. Median Mean 3rd Qu. Max.
>> 0.0000 0.4500 1.0000 1.6726 2.0000 140.0000
>> > length(hw11$y)
>> [1] 13765
>> > summary(y)
>> Min. 1st Qu. Median Mean 3rd Qu. Max.
>> 0.00000 0.00000 0.00000 0.24958 0.00000 1.00000
>> > length(y)
>> [1] 601
>> >
>>
>> > setwd("z:/homework")
>> > sink ("z:/homework/hw11.our", append=T, split=T)
>> > hw11 <- read.csv("ij10b.csv",header=T)
>> > hw11$yy <- hw11$e3
>> > attach(hw11)
>> > hw11$yy <- hw11$e3
>> > summary(hw11$yy)
>> Min. 1st Qu. Median Mean 3rd Qu. Max.
>> 0.0000 0.4500 1.0000 1.6726 2.0000 140.0000
>> > length(hw11$yy)
>> [1] 13765
>> > summary(yy)
>> Min. 1st Qu. Median Mean 3rd Qu. Max.
>> 0.0000 0.4500 1.0000 1.6726 2.0000 140.0000
>> > length(yy)
>> [1] 13765
>> >
>>
>
More information about the R-help
mailing list