R attach Function


attach() function makes the data available to the R Search Path.

attach(x)

x: dataframe, matrix, list

Following file has been used for ANOVA analysis:
(Download the data file)

Let first read in the data from the file:

>x <- read.csv("anova.csv",header=T,sep=",")


There are 3 variables, "Expression", "Gender" and "Subtype". We can display the variables by:

>x$Gender

[1] m m m m m f m m f m m f m m m m f m m m m m m f m m m f m m m m f m m m m
[38] m m m m m m m m m f m f m m m m m f m m f m m f m m m m f m m m m m m m m
[75] m m f m m m m m f m m m m m m m m m f m m f m m f m f m m f m m f m m f m
[112] m f m m f m m m f m m m f m f m f f f f f f m f m f f f m f f f f m f m f
[149] m f f m f f f f f m f m f f m f f m f f m f f f m f f f m f f f m f f m f
[186] f f m f f m f m m f m f m f f m f f f f f m f f m f f f m m m f m m m f f
[223] f f f f f m m m f m f f m f f f m f f f m f f f f m f m f f f f m f f f m
[260] f f m f f f f f f m f f m f f f f f f m f f
Levels: f m


We can't use the variable "Gender" in R Search Path:

>gender

Error: object 'Gender' not found


After attach the object "x", "Gender" can be used globally:

>attach(x)
>Gender

[1] m m m m m f m m f m m f m m m m f m m m m m m f m m m f m m m m f m m m m
[38] m m m m m m m m m f m f m m m m m f m m f m m f m m m m f m m m m m m m m
[75] m m f m m m m m f m m m m m m m m m f m m f m m f m f m m f m m f m m f m
[112] m f m m f m m m f m m m f m f m f f f f f f m f m f f f m f f f f m f m f
[149] m f f m f f f f f m f m f f m f f m f f m f f f m f f f m f f f m f f m f
[186] f f m f f m f m m f m f m f f m f f f f f m f f m f f f m m m f m m m f f
[223] f f f f f m m m f m f f m f f f m f f f m f f f f m f m f f f f m f f f m
[260] f f m f f f f f f m f f m f f f f f f m f f
Levels: f m


detach() function reverses the process:

>detach(x)
>Gender

Error: object 'Gender' not found

endmemo.com © 2024  | Terms of Use | Privacy | Home