[R] newbie: data frame to vector
Ko-Kang Kevin Wang
Ko-Kang at xtra.co.nz
Tue Jan 21 23:03:04 CET 2003
Hi,
----- Original Message -----
From: "Eric Peterson" <peterson at heritage.nv.gov>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, January 22, 2003 10:47 AM
Subject: [R] newbie: data frame to vector
> Sorry, but I'm very new to R. I'm trying to figure out how to convert a
> column from a data frame to a vector. More specifically, I have read in a
> comma separated value table which contains a number of variables in
columns,
> plots in rows...
>
> t <- read.table("G:/R/table1.txt", sep=",", header=TRUE)
>
> the data comes in just fine. Now I want to do an XY plot of 2 variables
and
> will be doing some statistical analyses on them. But
>
> plot(t["Var1"], t["Var2"])
I'm assuming your data frame, t, looks like:
Var1 Var2 Var3
1 2 3
4 5 6
7 8 9
Then there are (at least) two ways. The first is type:
attach(t)
right after your
> t <- read.table("G:/R/table1.txt", sep=",", header=TRUE)
this will allow you to use the column names directly, i.e. you can do:
plot(Var1, Var2)
If you do not attach it, you can still achieve your goal. Remember that the
data frame is two dimensional, to subset a two dimensional object you need a
comma, i.e
t[1, ] # give you row 1
t[, 1] # give you column 1
t[1, 1] # give you the first value in row 1, column 1
So to do what you want you need:
plot(t[, 1], t[, 2]) # Plot Column 1 and 2
Hope this helps,
Kevin Wang
------------------------------------------------
Ko-Kang Kevin Wang
Master of Science (MSc) Student
Department of Statistics
University of Auckland
New Zealand
www.stat.auckland.ac.nz/~kwan022
More information about the R-help
mailing list